Model validations, allow you to specify format/content/inheritance validations for each attribute of the model.

Validations are automatically run on create, update and save. You can also call validate() to manually validate an instance.

The validations are implemented by validator.js.

interface ColumnValidateOptions {
    contains?: string | {
        msg: string;
    };
    equals?: string | {
        msg: string;
    };
    is?: string | RegExp | readonly (string | RegExp)[] | {
        args: string | RegExp | readonly (string | RegExp)[];
        msg: string;
    };
    isAfter?: string | {
        args: string;
        msg: string;
    };
    isAlpha?: boolean | {
        msg: string;
    };
    isAlphanumeric?: boolean | {
        msg: string;
    };
    isArray?: boolean | {
        args: boolean;
        msg: string;
    };
    isBefore?: string | {
        args: string;
        msg: string;
    };
    isCreditCard?: boolean | {
        args: boolean;
        msg: string;
    };
    isDate?: boolean | {
        args: boolean;
        msg: string;
    };
    isDecimal?: boolean | {
        msg: string;
    };
    isEmail?: boolean | {
        msg: string;
    };
    isFloat?: boolean | {
        msg: string;
    };
    isIP?: boolean | {
        msg: string;
    };
    isIPv4?: boolean | {
        msg: string;
    };
    isIPv6?: boolean | {
        msg: string;
    };
    isIn?: readonly (readonly any[])[] | {
        args: readonly (readonly any[])[];
        msg: string;
    };
    isInt?: boolean | {
        msg: string;
    };
    isLowercase?: boolean | {
        msg: string;
    };
    isNull?: boolean | {
        msg: string;
    };
    isNumeric?: boolean | {
        msg: string;
    };
    isUUID?: number | {
        args: number;
        msg: string;
    };
    isUppercase?: boolean | {
        msg: string;
    };
    isUrl?: boolean | {
        msg: string;
    };
    len?: readonly [number, number] | {
        args: readonly [number, number];
        msg: string;
    };
    max?: number | {
        args: readonly [number];
        msg: string;
    };
    min?: number | {
        args: readonly [number];
        msg: string;
    };
    not?: string | RegExp | readonly (string | RegExp)[] | {
        args: string | RegExp | readonly (string | RegExp)[];
        msg: string;
    };
    notContains?: string | readonly string[] | {
        args: string | readonly string[];
        msg: string;
    };
    notEmpty?: boolean | {
        msg: string;
    };
    notIn?: readonly (readonly any[])[] | {
        args: readonly (readonly any[])[];
        msg: string;
    };
    notNull?: boolean | {
        msg: string;
    };
    [name: string]: unknown;
}

Indexable

[name: string]: unknown

Custom validations are also possible

Properties

contains?: string | {
    msg: string;
}

force specific substrings

Type declaration

  • msg: string
equals?: string | {
    msg: string;
}

only allow a specific value

Type declaration

  • msg: string
is?: string | RegExp | readonly (string | RegExp)[] | {
    args: string | RegExp | readonly (string | RegExp)[];
    msg: string;
}
  • { is: ['^[a-z]+$','i'] } will only allow letters
  • { is: /^[a-z]+$/i } also only allows letters

Type declaration

  • args: string | RegExp | readonly (string | RegExp)[]
  • msg: string
isAfter?: string | {
    args: string;
    msg: string;
}

only allow date strings after a specific date

Type declaration

  • args: string
  • msg: string
isAlpha?: boolean | {
    msg: string;
}

will only allow letters

Type declaration

  • msg: string
isAlphanumeric?: boolean | {
    msg: string;
}

will only allow alphanumeric characters, so "_abc" will fail

Type declaration

  • msg: string
isArray?: boolean | {
    args: boolean;
    msg: string;
}

only allow arrays

Type declaration

  • args: boolean
  • msg: string
isBefore?: string | {
    args: string;
    msg: string;
}

only allow date strings before a specific date

Type declaration

  • args: string
  • msg: string
isCreditCard?: boolean | {
    args: boolean;
    msg: string;
}

check for valid credit card numbers

Type declaration

  • args: boolean
  • msg: string
isDate?: boolean | {
    args: boolean;
    msg: string;
}

only allow date strings

Type declaration

  • args: boolean
  • msg: string
isDecimal?: boolean | {
    msg: string;
}

checks for any numbers

Type declaration

  • msg: string
isEmail?: boolean | {
    msg: string;
}

checks for email format ([email protected])

Type declaration

  • msg: string
isFloat?: boolean | {
    msg: string;
}

checks for valid floating point numbers

Type declaration

  • msg: string
isIP?: boolean | {
    msg: string;
}

checks for IPv4 (129.89.23.1) or IPv6 format

Type declaration

  • msg: string
isIPv4?: boolean | {
    msg: string;
}

checks for IPv4 (129.89.23.1)

Type declaration

  • msg: string
isIPv6?: boolean | {
    msg: string;
}

checks for IPv6 format

Type declaration

  • msg: string
isIn?: readonly (readonly any[])[] | {
    args: readonly (readonly any[])[];
    msg: string;
}

check the value is one of these

Type declaration

  • args: readonly (readonly any[])[]
  • msg: string
isInt?: boolean | {
    msg: string;
}

checks for valid integers

Type declaration

  • msg: string
isLowercase?: boolean | {
    msg: string;
}

checks for lowercase

Type declaration

  • msg: string
isNull?: boolean | {
    msg: string;
}

only allows null

Type declaration

  • msg: string
isNumeric?: boolean | {
    msg: string;
}

will only allow numbers

Type declaration

  • msg: string
isUUID?: number | {
    args: number;
    msg: string;
}

only allow uuids

Type declaration

  • args: number
  • msg: string
isUppercase?: boolean | {
    msg: string;
}

checks for uppercase

Type declaration

  • msg: string
isUrl?: boolean | {
    msg: string;
}

checks for url format (http://foo.com)

Type declaration

  • msg: string
len?: readonly [number, number] | {
    args: readonly [number, number];
    msg: string;
}

only allow values with length between 2 and 10

Type declaration

  • args: readonly [number, number]
  • msg: string
max?: number | {
    args: readonly [number];
    msg: string;
}

only allow values

Type declaration

  • args: readonly [number]
  • msg: string
min?: number | {
    args: readonly [number];
    msg: string;
}

only allow values >= 23

Type declaration

  • args: readonly [number]
  • msg: string
not?: string | RegExp | readonly (string | RegExp)[] | {
    args: string | RegExp | readonly (string | RegExp)[];
    msg: string;
}
  • { not: ['[a-z]','i'] } will not allow letters

Type declaration

  • args: string | RegExp | readonly (string | RegExp)[]
  • msg: string
notContains?: string | readonly string[] | {
    args: string | readonly string[];
    msg: string;
}

don't allow specific substrings

Type declaration

  • args: string | readonly string[]
  • msg: string
notEmpty?: boolean | {
    msg: string;
}

don't allow empty strings

Type declaration

  • msg: string
notIn?: readonly (readonly any[])[] | {
    args: readonly (readonly any[])[];
    msg: string;
}

check the value is not one of these

Type declaration

  • args: readonly (readonly any[])[]
  • msg: string
notNull?: boolean | {
    msg: string;
}

won't allow null

Type declaration

  • msg: string