Options
All
  • Public
  • Public/Protected
  • All
Menu

Class ObjectSchema

Schema to validate user defined objects.

Example

let schema = new ObjectSchema(false)
    .withOptionalProperty("id", TypeCode.String)
    .withRequiredProperty("name", TypeCode.String);

schema.validate({ id: "1", name: "ABC" });       // Result: no errors
schema.validate({ name: "ABC" });                // Result: no errors
schema.validate({ id: 1, name: "ABC" });         // Result: id type mismatch
schema.validate({ id: 1, _name: "ABC" });        // Result: name is missing, unexpected _name
schema.validate("ABC");                          // Result: type mismatch

Hierarchy

Index

Constructors

constructor

  • Creates a new validation schema and sets its values.

    see

    IValidationRule

    Parameters

    • Optional allowUndefined: boolean

      true to allow properties undefines in the schema

    • Optional required: boolean

      (optional) true to always require non-null values.

    • Optional rules: IValidationRule[]

      (optional) a list with validation rules.

    Returns ObjectSchema

Accessors

isUndefinedAllowed

  • get isUndefinedAllowed(): boolean
  • set isUndefinedAllowed(value: boolean): void
  • Gets flag to allow undefined properties

    Returns boolean

    true to allow undefined properties and false to disallow.

  • Sets flag to allow undefined properties

    Parameters

    • value: boolean

      true to allow undefined properties and false to disallow.

    Returns void

    true to allow undefined properties and false to disallow.

Methods

allowUndefined

  • Sets flag to allow undefined properties

    This method returns reference to this exception to implement Builder pattern to chain additional calls.

    Parameters

    • value: boolean

      true to allow undefined properties and false to disallow.

    Returns ObjectSchema

    this validation schema.

getProperties

  • Gets validation schemas for object properties.

    see

    PropertySchema

    Returns PropertySchema[]

    the list of property validation schemas.

getRules

  • Gets validation rules to check values against.

    Returns IValidationRule[]

    a list with validation rules.

isRequired

  • isRequired(): boolean
  • Gets a flag that always requires non-null values. For null values it raises a validation error.

    Returns boolean

    true to always require non-null values and false to allow null values.

makeOptional

  • Makes validated values optional. Validation for null values will be skipped.

    This method returns reference to this exception to implement Builder pattern to chain additional calls.

    see

    makeRequired

    Returns Schema

    this validation schema

makeRequired

  • Makes validated values always required (non-null). For null values the schema will raise errors.

    This method returns reference to this exception to implement Builder pattern to chain additional calls.

    see

    makeOptional

    Returns Schema

    this validation schema

Protected performTypeValidation

  • performTypeValidation(path: string, type: any, value: any, results: ValidationResult[]): void
  • Validates a given value to match specified type. The type can be defined as a Schema, type, a type name or TypeCode When type is a Schema, it executes validation recursively against that Schema.

    see

    performValidation

    Parameters

    • path: string

      a dot notation path to the value.

    • type: any

      a type to match the value type

    • value: any

      a value to be validated.

    • results: ValidationResult[]

      a list with validation results to add new results.

    Returns void

Protected performValidation

  • performValidation(path: string, value: any, results: ValidationResult[]): void
  • Validates a given value against the schema and configured validation rules.

    Parameters

    • path: string

      a dot notation path to the value.

    • value: any

      a value to be validated.

    • results: ValidationResult[]

      a list with validation results to add new results.

    Returns void

setProperties

  • Sets validation schemas for object properties.

    see

    PropertySchema

    Parameters

    Returns void

setRequired

  • setRequired(value: boolean): void
  • Sets a flag that always requires non-null values.

    Parameters

    • value: boolean

      true to always require non-null values and false to allow null values.

    Returns void

setRules

  • Sets validation rules to check values against.

    Parameters

    Returns void

validate

  • Validates the given value and results validation results.

    see

    ValidationResult

    Parameters

    • value: any

      a value to be validated.

    Returns ValidationResult[]

    a list with validation results.

validateAndReturnException

  • validateAndReturnException(correlationId: string, value: any, strict?: boolean): ValidationException
  • Validates the given value and returns a ValidationException if errors were found.

    Parameters

    • correlationId: string

      (optional) transaction id to trace execution through call chain.

    • value: any

      a value to be validated.

    • Default value strict: boolean = false

      true to treat warnings as errors.

    Returns ValidationException

validateAndThrowException

  • validateAndThrowException(correlationId: string, value: any, strict?: boolean): void

withOptionalProperty

  • Adds a validation schema for an optional object property.

    Parameters

    • name: string

      a property name.

    • Optional type: any

      (optional) a property schema or type.

    • Rest ...rules: IValidationRule[]

      (optional) a list of property validation rules.

    Returns ObjectSchema

withProperty

  • Adds a validation schema for an object property.

    This method returns reference to this exception to implement Builder pattern to chain additional calls.

    see

    PropertySchema

    Parameters

    Returns ObjectSchema

    this validation schema.

withRequiredProperty

  • Adds a validation schema for a required object property.

    Parameters

    • name: string

      a property name.

    • Optional type: any

      (optional) a property schema or type.

    • Rest ...rules: IValidationRule[]

      (optional) a list of property validation rules.

    Returns ObjectSchema

withRule

  • Adds validation rule to this schema.

    This method returns reference to this exception to implement Builder pattern to chain additional calls.

    Parameters

    Returns Schema

    this validation schema.

Generated using TypeDoc