AJSF API reference
    Preparing search index...

    Class JsonValidators

    'JsonValidators' class

    Provides an extended set of validators to be used by form controls, compatible with standard JSON Schema validation options. http://json-schema.org/latest/json-schema-validation.html

    Note: This library is designed as a drop-in replacement for the Angular Validators library, and except for one small breaking change to the 'pattern' validator (described below) it can even be imported as a substitute, like so:

    import { JsonValidators as Validators } from 'json-validators';

    and it should work with existing code as a complete replacement.

    The one exception is the 'pattern' validator, which has been changed to matche partial values by default (the standard 'pattern' validator wrapped all patterns in '^' and '$', forcing them to always match an entire value). However, the old behavior can be restored by simply adding '^' and '$' around your patterns, or by passing an optional second parameter of TRUE. This change is to make the 'pattern' validator match the behavior of a JSON Schema pattern, which allows partial matches, rather than the behavior of an HTML input control pattern, which does not.

    This library replaces Angular's validators and combination functions with the following validators and transformation functions:

    Validators: For all formControls: required (), type, enum, const For text formControls: minLength (), maxLength (), pattern (), format For numeric formControls: maximum, exclusiveMaximum, minimum, exclusiveMinimum, multipleOf For formGroup objects: minProperties, maxProperties, dependencies For formArray arrays: minItems, maxItems, uniqueItems, contains Not used by JSON Schema: min (), max (), requiredTrue (), email () (Validators originally included with Angular are maked with (*).)

    NOTE / TODO: The dependencies validator is not complete. NOTE / TODO: The contains validator is not complete.

    Validators not used by JSON Schema (but included for compatibility) and their JSON Schema equivalents:

    Angular validator JSON Schema equivalent
    min(number)     |   minimum(number)
    max(number)     |   maximum(number)
    requiredTrue()  |   const(true)
    email()         |   format('email')
    

    Validator transformation functions: composeAnyOf, composeOneOf, composeAllOf, composeNot (Angular's original combination funciton, 'compose', is also included for backward compatibility, though it is functionally equivalent to composeAllOf, asside from its more generic error message.)

    All validators have also been extended to accept an optional second argument which, if passed a TRUE value, causes the validator to perform the opposite of its original finction. (This is used internally to enable 'not' and 'composeOneOf' to function and return useful error messages.)

    The 'required' validator has also been overloaded so that if called with a boolean parameter (or no parameters) it returns the original validator function (rather than executing it). However, if it is called with an AbstractControl parameter (as was previously required), it behaves exactly as before.

    This enables all validators (including 'required') to be constructed in exactly the same way, so they can be automatically applied using the equivalent key names and values taken directly from a JSON Schema.

    This source code is partially derived from Angular, which is Copyright (c) 2014-2017 Google, Inc. Use of this source code is therefore governed by the same MIT-style license that can be found in the LICENSE file at https://angular.io/license

    Original Angular Validators: https://github.com/angular/angular/blob/master/packages/forms/src/validators.ts

    Index
    • 'composeAllOf' validator combination function

      Accepts an array of validators and returns a single validator that evaluates to valid only if all the submitted validators are individually valid. Otherwise it returns combined errors from all invalid validators.

      // {IValidatorFn[]} validators - array of validators to combine // {IValidatorFn} - single combined validator function

      Parameters

      Returns IValidatorFn

    • 'composeAnyOf' validator combination function

      Accepts an array of validators and returns a single validator that evaluates to valid if any one or more of the submitted validators are valid. If every validator is invalid, it returns combined errors from all validators.

      // {IValidatorFn[]} validators - array of validators to combine // {IValidatorFn} - single combined validator function

      Parameters

      Returns IValidatorFn

    • 'composeNot' validator inversion function

      Accepts a single validator function and inverts its result. Returns valid if the submitted validator is invalid, and returns invalid if the submitted validator is valid. (Note: this function can itself be inverted

      • e.g. composeNot(composeNot(validator)) - but this can be confusing and is therefore not recommended.)

      // {IValidatorFn[]} validators - validator(s) to invert // {IValidatorFn} - new validator function that returns opposite result

      Parameters

      Returns IValidatorFn

    • 'composeOneOf' validator combination function

      Accepts an array of validators and returns a single validator that evaluates to valid only if exactly one of the submitted validators is valid. Otherwise returns combined information from all validators, both valid and invalid.

      // {IValidatorFn[]} validators - array of validators to combine // {IValidatorFn} - single combined validator function

      Parameters

      Returns IValidatorFn

    • 'const' validator

      Requires a control to have a specific value.

      Converts types as needed to allow string inputs to still correctly match number, boolean, and null values.

      TODO: modify to work with objects

      // {any[]} requiredValue - required value // {IValidatorFn}

      Parameters

      • requiredValue: any

      Returns IValidatorFn

    • 'enum' validator

      Requires a control to have a value from an enumerated list of values.

      Converts types as needed to allow string inputs to still correctly match number, boolean, and null enum values.

      // {any[]} allowedValues - array of acceptable values // {IValidatorFn}

      Parameters

      • allowedValues: any[]

      Returns IValidatorFn

    • 'maxLength' validator

      Requires a control's text value to be less than a specified length.

      // {number} maximumLength - maximum allowed string length // {boolean = false} invert - instead return error object only if valid // {IValidatorFn}

      Parameters

      • maximumLength: number

      Returns IValidatorFn

    • 'maxProperties' validator

      Requires a form group to have a maximum number of properties (i.e. have values entered in a maximum number of controls within the group).

      Note: Has no effect if the form group does not contain more than the maximum number of controls.

      // {number} maximumProperties - maximum number of properties allowed // {IValidatorFn}

      Parameters

      • maximumProperties: number

      Returns IValidatorFn

    • 'minLength' validator

      Requires a control's text value to be greater than a specified length.

      // {number} minimumLength - minimum allowed string length // {boolean = false} invert - instead return error object only if valid // {IValidatorFn}

      Parameters

      • minimumLength: number

      Returns IValidatorFn

    • 'minProperties' validator

      Requires a form group to have a minimum number of properties (i.e. have values entered in a minimum number of controls within the group).

      // {number} minimumProperties - minimum number of properties allowed // {IValidatorFn}

      Parameters

      • minimumProperties: number

      Returns IValidatorFn

    • 'multipleOf' validator

      Requires a control to have a numeric value that is a multiple of a specified number.

      // {number} multipleOfValue - number value must be a multiple of // {IValidatorFn}

      Parameters

      • multipleOfValue: number

      Returns IValidatorFn

    • 'pattern' validator

      Note: NOT the same as Angular's default pattern validator.

      Requires a control's value to match a specified regular expression pattern.

      This validator changes the behavior of default pattern validator by replacing RegExp(^${pattern}$) with RegExp(${pattern}), which allows for partial matches.

      To return to the default funcitonality, and match the entire string, pass TRUE as the optional second parameter.

      // {string} pattern - regular expression pattern // {boolean = false} wholeString - match whole value string? // {IValidatorFn}

      Parameters

      • pattern: string | RegExp
      • wholeString: boolean = false

      Returns IValidatorFn

    • 'required' validator

      This validator is overloaded, compared to the default required validator. If called with no parameters, or TRUE, this validator returns the 'required' validator function (rather than executing it). This matches the behavior of all other validators in this library.

      If this validator is called with an AbstractControl parameter (as was previously required) it behaves the same as Angular's default required validator, and returns an error if the control is empty.

      Old behavior: (if input type = AbstractControl) // {AbstractControl} control - required control // {{[key: string]: boolean}} - returns error message if no input

      New behavior: (if no input, or input type = boolean) // {boolean = true} required? - true to validate, false to disable // {IValidatorFn} - returns the 'required' validator function itself

      Parameters

      • input: AbstractControl

      Returns ValidationErrors

    • 'required' validator

      This validator is overloaded, compared to the default required validator. If called with no parameters, or TRUE, this validator returns the 'required' validator function (rather than executing it). This matches the behavior of all other validators in this library.

      If this validator is called with an AbstractControl parameter (as was previously required) it behaves the same as Angular's default required validator, and returns an error if the control is empty.

      Old behavior: (if input type = AbstractControl) // {AbstractControl} control - required control // {{[key: string]: boolean}} - returns error message if no input

      New behavior: (if no input, or input type = boolean) // {boolean = true} required? - true to validate, false to disable // {IValidatorFn} - returns the 'required' validator function itself

      Parameters

      • Optionalinput: boolean

      Returns IValidatorFn