idb-ts
    Preparing search index...

    Function Validate

    • Property decorator that attaches a validation rule to the decorated field. The rule is enforced on every EntityRepository.create and EntityRepository.update call.

      Type Parameters

      • T = any

        The entity class to which this rule belongs.

      Parameters

      • predicate: (value: any, item: T) => boolean

        A function that returns true when the field value is valid. Receives both the field value and the full entity instance.

      • message: string

        A short, human-readable description of the constraint, appended to the thrown error on failure.

      Returns PropertyDecorator

      Error - On write operations when any rule's predicate returns false. The error message lists all failing rules in the format field: message, joined by ; .

      @DataClass()
      class User {
      @KeyPath()
      id!: string;

      @Validate((v) => typeof v === 'string' && v.includes('@'), 'must be a valid email')
      email!: string;

      @Validate((v) => typeof v === 'number' && v >= 0, 'age must be >= 0')
      age!: number;
      }