Validation

More often than not you will have the need to validate some data that is passed to your plugin, like configuration or data that should be persisted where an invalid format could have serious consequences.

The default validator that ships with Core implements @hapi/joi under the hood to provide an easy to use syntax for building validation schemas.

Usage

Get an instance of the Validator

1const validator: Services.Validation.ValidationService = app
2 .get<Contracts.Kernel.Validator>(Identifiers.Services.Validation.Service)

Run the validator’s rules against its data

1validator.validate({ username: "johndoe" }, Joi.object({ username: Joi.string() }));

Determine if the data passes the validation rules

1validator.passes();

Determine if the data fails the validation rules

1validator.fails();

Get the failed validation rules

1validator.failed();

Get all of the validation error messages

1validator.errors();

Returns the data which was valid

1validator.valid();

Returns the data which was invalid

1validator.invalid();

Get the data under validation

1validator.attributes();
Last updated 2 months ago
Edit Page
Share: