Create Validator
In @bshg/validation
the api that is responsible of validate the data is called Validator
, this one is provide a lot of build in functionality to do the validation and more.
The v.validator()
function is the core method that allows you to create validators for your data types. As seen in the previous Basic Usage example with the User
type, this function requires the data type and accepts a configuration object as a parameter.
Function Signature
function validator<Type, TContext>(config: ValidatorConfig<Type, TContext>): Validator<Type, TContext> {}
Config Param: ValidatorConfig
The configuration object for v.validator()
has the following structure:
type ValidatorConfig<TV, TContext> = {
id?: string,
items?: ItemType<TV, TContext>,
nested?: NestedType<TV>,
options?: ValidatorOptions
}
Property | Type | Description |
---|---|---|
id | string | An optional identifier for the validator. This can be useful if you need to distinguish between multiple validators in your application. It is used to differentiate between the logs of the validators. |
items | ItemType<TV, TContext> | Defines the validation rules for the individual fields of your data type. This is where you specify the constraints and requirements for each property of your data type. |
nested | NestedType<TV> | Allows you to define nested validation rules if your data type contains nested objects that also need validation. |
options | ValidatorOptions | Additional options to configure the behavior of the validator. |
Note:
TContext
is used to pass a type of the context.