lanka-validations - v2.1.0
    Preparing search index...

    Class SchemaValidator<TInput, TOutput>

    Validates a form object field by field. Create one with createSchemaValidator, or with new SchemaValidator() and SchemaValidator.field.

    Type Parameters

    • TInput extends Record<string, unknown> = Record<string, unknown>
    • TOutput extends Record<string, unknown> = TInput
    Index
    • Adds rules to a field, after any it already has. Returns the validator for chaining.

      Parameters

      • name: keyof TInput

        Field name in the form object.

      • ...rules: ValidatorRule<unknown>[]

        Rules to append, checked in order.

      Returns this

      The same validator.

      const form = new SchemaValidator().field('nic', rules.required(), rules.nic());
      form.validate({}).errorCodes; // { nic: 'REQUIRED' }
    • Checks every field. Each field stops at its first failing rule. Non-object input, arrays, and class instances fail with code SCHEMA_INVALID_ROOT instead of throwing.

      Parameters

      • data: unknown

        Form data to check, usually a parsed request body.

      Returns SchemaValidationResult<TOutput>

      { isValid: true, data } with every field's parsed value, or { isValid: false, data } with only the fields that passed. Both include errors (messages), errorCodes, and errorList, keyed by field.