Options
All
  • Public
  • Public/Protected
  • All
Menu

Class ApiValidator

Class which allows to validate data that is coming to API endpoints.
Internally it uses ajv in order to validate JSON objects.
XSS sanitization is performed with the xss npm package.

Hierarchy

  • ApiValidator

Constructors

constructor

Methods

init

  • init(validationSchemasDir?: string, excludeDirs?: string[]): Promise<void>
  • Initializes ApiValidator and reads validation JSON Schemas. Each JSON schema needs to have an id of this format: #${service}-${method}.
    JSON schemas needs to be located on the file system in the following topology:

    └─ <${validationSchemasDir}>
        └─ service-1
        │  ├─ schema-1.json
        │  └─ schema-2.json
        ├─ service-2
        │  ├─ schema-1.json
        │  ├─ schema-2.json
        │  └─ schema-n.json
        └─ service-n
             └─ schema-1.json

    Parameters

    • Optional validationSchemasDir: string

      Directory where validation schemas are located.
      Defaults to ${process.env['XDG_CONFIG_HOME'] || ${process.env['HOME']}/.config}/${process.env['APP_NAME']}/validation.

    • Optional excludeDirs: string[]

      Directories from the validationSchemasDir which needs to be excluded, i.e. their schemas should not be loaded.

    Returns Promise<void>

joinErrors

  • joinErrors(errors: (ErrorObject<string, Record<string, any>, unknown> | Partial<ErrorObject<string, Record<string, any>, unknown>>)[], into: "object" | "text", skippedKeywords?: string[]): string | ObjMap
  • Joins errors from the exception thrown by ApiValidator.validate method.
    Errors can be joined into text or object.
    When text is specified, errors will be joined into message using ajv-i18n.
    When json is specified, errors will be joined into an object, having as key instancePath error property and as value message error property.

    Parameters

    • errors: (ErrorObject<string, Record<string, any>, unknown> | Partial<ErrorObject<string, Record<string, any>, unknown>>)[]

      (https://ajv.js.org/api.html#error-objects) from the exception thrown by ApiValidator.validate method.

    • into: "object" | "text"

      Format into which errors need to be joined.

    • Optional skippedKeywords: string[]

      When format is json, you can skip some error objects having keyword property present in this list.

    Returns string | ObjMap

    Joined errors.

sanitize

  • sanitize(data: string | ObjMap, exceptPaths?: Set<string>): string | ObjMap
  • Sanitizes data against XSS vulnerability.
    Notice that in case of JSON data, only values will be sanitized, while keys will be left untouched.

    Parameters

    • data: string | ObjMap

      Data to be sanitized.

    • Optional exceptPaths: Set<string>

      When data is an object, you can specify a set of dot paths, values of which should not be sanitized.

    Returns string | ObjMap

    Sanitized data.

validate

  • validate(service: string, method: string, data: ObjMap): Promise<ObjMap>
  • Validate data against JSON schema. Id of the schema is formed from #${service}-${method}.

    throws

    {ErrorObject} When data doesn't match schema.

    Parameters

    • service: string

      Name of the service.

    • method: string

      Name of the method.

    • data: ObjMap

      Data to be validated.

    Returns Promise<ObjMap>

    Validated data.