ProtectedisWhether the schema requires a non-null/non-undefined value.
Sets the requirement flag. Must be a boolean.
ProtectedpreprocessorsA list of preprocessors associated with the Builder
ProtectedtypeThe string identifier of the schema type (e.g. 'string', 'number', 'object').
Sets the schema type identifier. Must be a non-empty string.
ProtectedvalidatorsA list of validators associated with the Builder
Fields not defined in properties will not be validated
and will be passed through the validation.
Adds a preprocessor to a preprocessors list
Adds a new property to the object schema. The new property will be validated according to the provided schema.
Adds new properties to the object schema. The same as .addProp() but
allows to add multiple properties with one call. The new properties
will be validated according to the provided schemas.
a key/schema object map.
Adds all properties from the schema object schema to the current schema.
an instance of ObjectSchemaBuilder
Adds a validator to validators list.
ProtectedassureEnsures a ValidationErrorMessageProvider is valid.
If provider is undefined, falls back to defaultValue.
Function providers are bound to this for access to schema state.
the provider to validate, or undefined
fallback provider when provider is not supplied
a valid ValidationErrorMessageProvider
Clears type set by call to .hasType<T>(), default schema type inference will be used
for schema returned by this call.
Remove all preprocessors for this schema.
Remove all validators for this schema.
ProtectedcreateProtected method used to create a new instance of the Builder
defined by the props object. Should be used to instantiate new
builders to keep builder's immutability.
ProtectedgetResolves a ValidationErrorMessageProvider to a string error message.
Handles both string providers and function providers (sync or async).
the error message provider (string or function)
the value that caused the validation error
the resolved error message string
Set type of schema explicitly. notUsed param is needed only for case when JS is used. E.g. when you
can't call method like schema.hasType<Date>(), so instead you can call schema.hasType(new Date())
with the same result.
Optional_notUsed: TAdds all properties from schema to the current schema.
TSchema & TAnotherSchema is a good example of the similar concept
in the TS type system.
an object schema to take properties from
Generates a serializable object describing the defined schema
If set to true, schema validation will not
return errors if object contains fields which
are not defined in the schema properties.
Set to false by default
If set to false, schema will be optional (null or undefined values
will be considered as valid).
Array of preprocessor functions
Properties defined in schema
Custom error message provider for the 'is required' validation error.
String id of schema type, e.g. string', numberorobject`.
Array of validator functions
Partial<T> would be a good example of the
same operation in the TS world.
Required<T> would be a good example of the
same operation in the TS world.
An alias for .partial(prop: string)
name of the property
Marks prop as required property.
If prop does not exists in the current schema,
an error will be thrown.
name of the property
Modify schema for propName and return a new schema.
Could be useful if you want to leave all schema intact, but
change a type of one property.
name of the property (string)
callback function returning a new schema fo the propName. As a first parameter
you will receive an old schema for propName.
Fields not defined in properties will be considered
as schema violation. This is the default behavior.
Omits properties listed in properties from the schema.
Consider Omit<Type, 'prop1'|'prop2'...> as a good illustration
from the TS world.
array of property names (strings) to remove from the schema.
Removes propName from the list of properties.
property name to remove. Schema should contain this property. An error will be thrown otherwise.
Removes all properties of schema from the current schema.
Omit<TSchema, keyof TAnotherSchema> as a good illustration
from the TS world.
schema builder to take properties from.
Marks all properties in the current schema as optional.
It is the same as call .optional('propname') where propname is the name
of every property in the schema.
Marks all properties from properties as optional in the schema.
list of property names (string) to make optional
Marks property propName as optional in the schema.
the name of the property (string).
Returns a new schema containing only properties listed in
properties array.
array of property names (strings)
Returns new schema based on the current schema. This new schema
will consists only from properties which names are taken from the
schema object schema.
schema to take property names list from
Returns a new schema consisting of only one property
(taken from the property property name). If the property
does not exists in the current schema, an error will be thrown.
the name of the property (string).
ProtectedpreRuns preprocessors, validators, and the required/optional check on object.
Subclasses call this at the start of their validate() implementation to get
a preprocessed value wrapped in a transaction, along with any early errors.
the value to pre-validate
Optionalcontext: ValidationContext<ObjectSchemaBuilder<TProperties, TRequired, TExplicitType>>optional validation context settings
a PreValidationResult containing the preprocessed transaction, context, and any errors
Performs validation of object schema over the object.
The returned result includes a getErrorsFor() method for type-safe,
per-property error inspection. The flat errors array is deprecated
and will be removed in a future major version — use getErrorsFor() instead.
The object to validate against this schema.
Optionalcontext: ValidationContext<ObjectSchemaBuilder<TProperties, TRequired, TExplicitType>>Optional ValidationContext settings.
StaticgetStaticis
Object schema builder class. Similar to the
objecttype in JS. Allows to define a schema forobjectvalue. Should be used to validate objects with specific properties. Properties should be defined as their own schema builders. You can use anySchemaBuildere.g.string(),number(),boolean(),array(),object(), etc. to define properties. Which means that you can define nested objects and arrays of any complexity.NOTE this class is exported only to give opportunity to extend it by inheriting. It is not recommended to create an instance of this class directly. Use object() function instead.
Example
Example
Example
Example
See
object