Validation Configurations
Validation configurations are used to set the default validation rules, errors strategy and other configurations.
Define rules list
Let's go to our src/config/validation.ts file and define the rules list.
src/config/validation.ts
import {
RequiredRule,
UniqueRule,
ValidationConfigurations,
} from "@mongez/warlock";
const validationConfigurations: ValidationConfigurations = {
stopOnFirstFailure: true,
returnErrorStrategy: "first",
responseStatus: 400,
rules: {
required: RequiredRule,
unique: UniqueRule,
},
keys: {
response: "messages",
inputKey: "key",
inputError: "error",
inputErrors: "errors",
},
};
export default validationConfigurations;
Lets break down the above code:
stopOnFirstFailure: Stop the validation process on the first failure so it won't continue to validate the rest of the rules for the same input, default totrue.returnErrorStrategy: ifstopOnFIrstFailureis set tofalse, then you can decide what errors to be returned, for example if the input has 3 errors, you can return the first error orallerrors, default tofirst.responseStatus: The response status code to be returned when validation fails, default to400.rules: Rules list that will be used in thevalidation.rulesas astring, Warlock has a+30different rules that will cover most of the validation cases, but you can add your own rules though.keys: The keys used to return the errors, you can change it to whatever you want.keys.response: The key used to return the errors, default tomessages.keys.inputKey: The key used to return the input key, default tokey.keys.inputError: The key used to return the input error, default toerror.keys.inputErrors: The key used to return the input errors, default toerrors.