Fake Mirrors
    Preparing search index...

    Class AjvValidator

    Ajv-based validator implementation.

    Provides thread-safe JSON Schema validation with comprehensive error reporting. Uses the Ajv library as the underlying validation engine.

    https://ajv.js.org/ - Ajv documentation

    import { DIContainer } from '@famir/common'
    import {
    VALIDATOR,
    Validator,
    AjvValidator,
    ValidatorError,
    JSONSchemaType,
    } from '@famir/validator'

    // Get the container singleton.
    const container = DIContainer.getInstance()

    // Register dependency in container
    AjvValidator.register(container)

    // Resolve dependency from container
    const validator = container.resolve<Validator>(VALIDATOR)

    // Define your user interface
    interface User {
    name: string
    age: number
    }

    // Define your user schema
    const userSchema: JSONSchemaType<User> = {
    type: 'object',
    required: ['name', 'age'],
    properties: {
    name: { type: 'string' },
    age: { type: 'number', minimum: 0 }
    },
    additionalProperties: false,
    } as const

    // Add schema to validator
    validator.addSchema('user', userSchema)

    // Any data to validate
    const data: unknown = { name: 'John', age: 30 }

    // Type-safe guard validation
    if (validator.guardSchema<User>('user', data)) {
    // TypeScript knows this is User
    console.log(data.name)
    } else {
    console.warn('Invalid user data')
    }

    // Type-safe assert validation with exception filter
    try {
    validator.assertSchema<User>('user', data)
    // TypeScript knows this is User
    console.log(data.age)
    } catch (error) {
    if (error instanceof ValidatorError) {
    console.warn(error.validateErrors)
    } else {
    console.error(error)
    }
    }

    Implements

    • Registers a JSON Schema with a unique name.

      The schema will be compiled and cached for later validation.

      Parameters

      • name: string

        The unique identifier for the schema.

      • schema: object

        The JSON Schema object.

      Returns this

      This validator for method chaining.

      Error If a schema with the given name is already registered.

    • Validates data against a JSON Schema, acting as a type assertion.

      On failure, it throws a detailed validation error.

      Type Parameters

      • T

        The expected type after validation.

      Parameters

      • name: string

        The name of the schema to validate against.

      • data: unknown

        The data to be validated.

      Returns asserts data is T

      ValidatorError If validation fails, containing detailed error information.

    • Validates data against a JSON Schema, acting as a type guard.

      On success, it narrows the type of the data to the specified type T.

      Type Parameters

      • T

        The expected type after validation.

      Parameters

      • name: string

        The name of the schema to validate against.

      • data: unknown

        The data to be validated.

      Returns data is T

      true if the data is valid, false otherwise.