Class StandardValidator<S>

Non-compiling validator for standard TypeBox values.

Type Parameters

  • S extends TSchema

Hierarchy

Constructors

Properties

schema: Readonly<S>

JSON schema against which to validate values. When a schema provides an errorMessage string option, all errors occurring for that schema (but not for nested schemas) collapse into a single error having this message. The errorMessage option allows you to provide a custom error message for a schema. For example, an errorMessage on a schema for a property of an object replaces TypeBox's built-in error messages for errors that occur on that property.

Methods

  • Validates a value against the schema, halting validation at the first validation error with a ValidationException. Does not clean values of unrecognized properties.

    Throws

    ValidationException when the value is invalid, reporting only the first validation error in the details property. The errorMessage parameter provides the exception's overall error message.

    Parameters

    • value: Readonly<unknown>

      Value to validate against the schema.

    • Optional overallError: string

      Overall eror message to report in the exception. The substring {error}, if present, will be replaced with a string representation of the error. Defaults to "Invalid value".

    Returns void

  • Validates a value against the schema, halting validation at the first validation error with a ValidationException. On successful validation, removes unrecognized properties from the provided value.

    Throws

    ValidationException when the value is invalid, reporting only the first validation error in the details property. The errorMessage parameter provides the exception's overall error message.

    Parameters

    • value: unknown

      Value to validate against the schema.

    • Optional overallError: string

      Overall eror message to report in the exception. The substring {error}, if present, will be replaced with a string representation of the error. Defaults to "Invalid value".

    Returns void

  • Validates a value against the schema, halting validation at the first validation error with a ValidationException. On successful validation, returns a copy of the value with unrecognized properties removed, but returns the original value if there are no unrecognized properties.

    Throws

    ValidationException when the value is invalid, reporting only the first validation error in the details property. The errorMessage parameter provides the exception's overall error message.

    Parameters

    • value: Readonly<unknown>

      Value to validate against the schema.

    • Optional overallError: string

      Overall eror message to report in the exception. The substring {error}, if present, will be replaced with a string representation of the error. Defaults to "Invalid value".

    Returns Static<S, []>

    The provided value itself if the value is not an object or if the value is an object having no unrecognized properties. If the value is an object having at least one unrecognized property, returns a copy of the value with unrecognized properties removed.

  • Validates a value against the schema and returns an iteratable whose iterator yields the validation errors. The iterator tests the value for the next error on each call to next(), returning a ValueError for the error until done. It does not evaluate errors in advance of their being requested, allowing you to short-circuit validation by stopping iteration early. For performance reasons, it is best to call test() before calling this method. This method does not throw ValidationException and does not clean values of unrecognized properties.

    Parameters

    • value: Readonly<unknown>

      Value to validate against the schema.

    Returns Iterable<ValueError>

    An iteratable yielding all validation errors. Upon detecting one or more errors for a particular schema (possibly a nested schema), if the schema provides an errorMessage property, only a single error is reported for the schema, and the message property of this error is set to errorMessage's value. Also, the TypeBox error "Expected required property" is dropped when at least one other error is reported for the property. Consequently, only the Type.Any and Type.Unknown schemas can yield "Expected required property" errors.

  • Validates a value against the schema and returns the first error, returning null if there is no error. No validation is performed beyond the first error, allowing you to protect the server from wasting time and memory validating excessively long strings. It is equivalent to calling next() exactly once on the iterator returned by errors(), serving only as a convenience method. For performance reasons, it is best to call test() before calling this method. This method does not throw ValidationException and does not clean values of unrecognized properties.

    Parameters

    • value: Readonly<unknown>

      Value to validate against the schema.

    Returns null | ValueError

    The first validation error, if there is a validation error, otherwise null.

  • Tests whether a value conforms to the schema. For performance reasons, it is best to call this method before calling errors() or firstError(), should you also need to information about the errors. This method does not throw ValidationException and does not clean values of unrecognized properties.

    Parameters

    • value: Readonly<unknown>

      Value to validate against the schema.

    Returns boolean

    true when the value conforms to the schema, false otherwise.

  • Tests whether a value conforms to the schema, returning an iterable whose iterator yields the validation errors, or returning null if there are no validation errors. This method is equivalent to calling test() and then errors() and exists only for convenience. The method does not throw ValidationException and does not clean values of unrecognized properties.

    Parameters

    • value: Readonly<unknown>

      Value to validate against the schema.

    Returns null | Iterable<ValueError>

    An iteratable yielding all validation errors, if any, otherwise null. Upon detecting one or more errors for a particular schema (possibly a nested schema), if the schema provides an errorMessage property, only a single error is reported for the schema, and the message property of this error is set to errorMessage's value. Also, the TypeBox error "Expected required property" is dropped when at least one other error is reported for the property. Consequently, only the Type.Any and Type.Unknown schemas can yield "Expected required property" errors.

  • Tests whether a value conforms to the schema, returning the first error, or returning null if there is no error. This method is equivalent to calling test() and then firstError() and exists only for convenience. The method does not throw ValidationException and does not clean values of unrecognized properties.

    Parameters

    • value: Readonly<unknown>

      Value to validate against the schema.

    Returns null | ValueError

    The first validation error, if there is a validation error, otherwise null.

  • Validates a value against the schema, detecting all validation errors and reporting them by throwing ValidationException. Does not clean values of unrecognized properties.

    Throws

    ValidationException when the value is invalid, reporting all validation errors in the details property. The errorMessage parameter provides the exception's overall error message.

    Parameters

    • value: Readonly<unknown>

      Value to validate against the schema.

    • Optional overallError: string

      Overall error message to report in the exception.

    Returns void

  • Validates a value against the schema, detecting all validation errors and reporting them by throwing ValidationException. On successful validation, removes unrecognized properties from the provided value.

    Throws

    ValidationException when the value is invalid, reporting all validation errors in the details property. The errorMessage parameter provides the exception's overall error message.

    Parameters

    • value: unknown

      Value to validate against the schema.

    • Optional overallError: string

      Overall error message to report in the exception.=

    Returns void

  • Validates a value against the schema, detecting all validation errors and reporting them with a ValidationException. On successful validation, returns a copy of the value with unrecognized properties removed, but returns the original value if there are no unrecognized properties.

    Throws

    ValidationException when the value is invalid, reporting all validation errors in the details property. The errorMessage parameter provides the exception's overall error message.

    Parameters

    • value: Readonly<unknown>

      Value to validate against the schema.

    • Optional overallError: string

      Overall error message to report in the exception.

    Returns Static<S, []>

    The provided value itself if the value is not an object or if the value is an object having no unrecognized properties. If the value is an object having at least one unrecognized property, returns a copy of the value with unrecognized properties removed.

Generated using TypeDoc