@samatawy/checks
    Preparing search index...

    Class ObjectCheck

    Validates object-shaped input and coordinates checks for its fields.

    Use this class when the input should be an object and individual fields need their own validation rules.

    const result = await ObjectCheck.for({ name: 'Ada' })
    .check(checker => [
    checker.required('name').notEmpty()
    ])
    .then(checker => checker.result({ nested: true }));

    Implements

    Index

    Constructors

    • Creates an object checker for a value or for a named field within a parent object.

      When both key and data are provided, the checker reads data[key].

      Parameters

      • key: string | number | null | undefined
      • data: any

      Returns ObjectCheck

    Properties

    key: string | number | null | undefined
    data: any
    has_value: boolean
    is_object: boolean
    check_extra_fields: boolean
    known_keys: Set<string>

    Methods

    • Requires the value to exist and contain at least one key.

      Parameters

      Returns this

      const result = ObjectCheck.for({}).notEmpty().result();
      
    • Returns a checker for an optional field and marks that field as known.

      Parameters

      • name: string

      Returns FieldCheck

      const nicknameCheck = ObjectCheck.for({}).optional('nickname');
      
    • Returns a field checker that becomes required only when condition passes.

      The condition receives the current object value.

      Parameters

      • name: string
      • condition: (data: any) => boolean
      • Optionaloptions: CheckOptions

      Returns FieldCheck

      const checker = ObjectCheck.for({ type: 'person' });
      checker.conditional('name', data => data.type === 'person').notEmpty();
    • Runs a group of field or nested checks and merges their results.

      Return an array of checks from the callback. Each check can be synchronous or a promise.

      Parameters

      Returns Promise<ObjectCheck>

      const checker = await ObjectCheck.for({ name: 'Ada', age: 37 }).check(c => [
      c.required('name').notEmpty(),
      c.optional('age').number()
      ]);
    • Evaluates alternative branches and succeeds when at least one branch is valid.

      Each branch function is evaluated in isolation using cloned object state. Valid branches are then replayed on the current checker so mutations behave the same way as normal non-branch checks.

      Parameters

      Returns Promise<ObjectCheck>

    • Evaluates alternative branches and succeeds only when exactly one branch is valid.

      Each branch function is evaluated in isolation using cloned object state. The single winning branch is then replayed on the current checker so mutations behave the same way as normal non-branch checks.

      Parameters

      Returns Promise<ObjectCheck>

    • Validates the current object value against a class definition. *

      • This merges the class-validation result into the current checker so it can
      • be composed with fluent object rules.

      Type Parameters

      • T

      Parameters

      Returns Promise<ObjectCheck>

      • 
        
      • const checker = await ObjectCheck.for(payload).matchesType(PersonDto);
      • 
        
    • Applies a custom predicate to the current object value.

      When the predicate returns false, the result contains Custom check failed unless custom check options override the message or code.

      Parameters

      • func: (data: any) => boolean | Promise<boolean>
      • Optionaloptions: CheckOptions

      Returns Promise<ObjectCheck>

      const checker = await ObjectCheck.for({ role: 'admin' })
      .isTrue(data => data.role === 'admin');
    • Merges a prior result into this checker when that prior result is invalid.

      This is useful when composing validations from external sources.

      Parameters

      Returns this

    • Returns the validation result for the current object.

      With no options, this returns the raw internal result structure. With options, the output is formatted through the package result helpers.

      Parameters

      Returns IResult

      const result = await ObjectCheck.for({ name: 'Ada' })
      .check(c => [c.required('name').notEmpty()])
      .then(c => c.result({ nested: true }));