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

    Function createSchemaValidator

    • Creates a SchemaValidator from a map of field names to rules. The parsed output type is inferred from the rules.

      Type Parameters

      Parameters

      • schemaMap: S

        Field names mapped to one rule or an ordered list of rules. Use the built-in rules or your own functions.

      Returns SchemaValidator<Record<keyof S, unknown>, InferSchemaOutput<S>>

      A validator whose validate(data) returns typed, parsed data or per-field errors.

      const form = createSchemaValidator({ nic: rules.nic(), phone: rules.optional(rules.mobile()) });

      const ok = form.validate({ nic: '921234567V', phone: '0771234567' });
      ok.isValid; // true
      if (ok.isValid) ok.data.nic.dobString; // '1992-05-02'

      form.validate({ nic: 'bad' }).errorCodes; // { nic: 'NIC_INVALID_FORMAT' }
      form.validate(null).errorCodes; // { $root: 'SCHEMA_INVALID_ROOT' }
    • Creates a SchemaValidator from a map of field names to rules. The parsed output type is inferred from the rules.

      Type Parameters

      • T extends Record<string, unknown> = Record<string, unknown>

      Parameters

      • OptionalschemaMap: Partial<
            Record<
                keyof T,
                ValidatorRule<unknown>
                | readonly ValidatorRule<unknown>[],
            >,
        >

        Field names mapped to one rule or an ordered list of rules. Use the built-in rules or your own functions.

      Returns SchemaValidator<T, T>

      A validator whose validate(data) returns typed, parsed data or per-field errors.

      const form = createSchemaValidator({ nic: rules.nic(), phone: rules.optional(rules.mobile()) });

      const ok = form.validate({ nic: '921234567V', phone: '0771234567' });
      ok.isValid; // true
      if (ok.isValid) ok.data.nic.dobString; // '1992-05-02'

      form.validate({ nic: 'bad' }).errorCodes; // { nic: 'NIC_INVALID_FORMAT' }
      form.validate(null).errorCodes; // { $root: 'SCHEMA_INVALID_ROOT' }