<commit_before>// std
import { ValidateFunction } from 'ajv';

// FoalTS
import {
  ApiParameter,
  ApiResponse,
  Context,
  Hook,
  HookDecorator,
  HttpResponseBadRequest,
  IApiQueryParameter,
  OpenApi,
  ServiceManager
} from '../../core';
import { getAjvInstance } from './get-ajv-instance';
import { isFunction } from '../helpers';

/**
 * Hook - Validate a specific query parameter against an AJV schema.
 *
 * @export
 * @param {string} name - Query parameter name.
 * @param {(object | ((controller: any) => object))} [schema={ type: 'string' }] - Schema used to
 * validate the query parameter.
 * @param {{ openapi?: boolean, required?: boolean }} [options={}] - Options.
 * @param {boolean} [options.openapi] - Add OpenApi metadata.
 * @param {boolean} [options.required] - Specify is the query is optional.
 * @returns {HookDecorator} The hook.
 */
export function ValidateQueryParam(
  name: string,
  schema: object | ((controller: any) => object) = { type: 'string' },
  options: { openapi?: boolean, required?: boolean } = {}
): HookDecorator {
  // tslint:disable-next-line
  const required = options.required ?? true;

  let validateSchema: ValidateFunction|undefined;

  function validate(this: any, ctx: Context, services: ServiceManager) {
    if (!validateSchema) {
      const ajvSchema = isFunction(schema) ? schema(this) : schema;
      const components = services.get(OpenApi).getComponents(this);

      validateSchema = getAjvInstance().compile({
        components,
        properties: {
          [name]: ajvSchema
        },
        required: required ? [ name ] : [],
        type: 'object',
      });
    }

    if (!validateSchema(ctx.request.query)) {
      return new HttpResponseBadRequest({ query: validateSchema.errors });
    }
  }

  const param: IApiQueryParameter = { in: 'query', name };
  if (required) {
    param.required = required;
  }

  const openapi = [
    ApiParameter((c: any) => ({
      ...param,
      schema: isFunction(schema) ? schema(c) : schema
    })),
    ApiResponse(400, { description: 'Bad request.' })
  ];

  return Hook(validate, openapi, options);
}
<commit_msg>Fix bad import<commit_after>// std
import { ValidateFunction } from 'ajv';

// FoalTS
import {
  ApiParameter,
  ApiResponse,
  Context,
  Hook,
  HookDecorator,
  HttpResponseBadRequest,
  IApiQueryParameter,
  OpenApi,
  ServiceManager
} from '../../core';
import { getAjvInstance } from './get-ajv-instance';
import { isFunction } from './helpers';

/**
 * Hook - Validate a specific query parameter against an AJV schema.
 *
 * @export
 * @param {string} name - Query parameter name.
 * @param {(object | ((controller: any) => object))} [schema={ type: 'string' }] - Schema used to
 * validate the query parameter.
 * @param {{ openapi?: boolean, required?: boolean }} [options={}] - Options.
 * @param {boolean} [options.openapi] - Add OpenApi metadata.
 * @param {boolean} [options.required] - Specify is the query is optional.
 * @returns {HookDecorator} The hook.
 */
export function ValidateQueryParam(
  name: string,
  schema: object | ((controller: any) => object) = { type: 'string' },
  options: { openapi?: boolean, required?: boolean } = {}
): HookDecorator {
  // tslint:disable-next-line
  const required = options.required ?? true;

  let validateSchema: ValidateFunction|undefined;

  function validate(this: any, ctx: Context, services: ServiceManager) {
    if (!validateSchema) {
      const ajvSchema = isFunction(schema) ? schema(this) : schema;
      const components = services.get(OpenApi).getComponents(this);

      validateSchema = getAjvInstance().compile({
        components,
        properties: {
          [name]: ajvSchema
        },
        required: required ? [ name ] : [],
        type: 'object',
      });
    }

    if (!validateSchema(ctx.request.query)) {
      return new HttpResponseBadRequest({ query: validateSchema.errors });
    }
  }

  const param: IApiQueryParameter = { in: 'query', name };
  if (required) {
    param.required = required;
  }

  const openapi = [
    ApiParameter((c: any) => ({
      ...param,
      schema: isFunction(schema) ? schema(c) : schema
    })),
    ApiResponse(400, { description: 'Bad request.' })
  ];

  return Hook(validate, openapi, options);
}

{'fraction_non_alphanumeric': 0.14482919981282172, 'fraction_numerical': 0.0014038371548900327, 'mean_word_length': 3.300804828973843, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7320565', 'n_tokens_mistral': 1271, 'n_tokens_neox': 1260, 'n_words': 348}