Default Configuration
Valid8r provides a way to default all your validation configuration globally for your project in on go, you just need to provide all the configuration for all the fields in one place.
Once you’ve configured globally for all the fields, every validation in your project will by default follow that configuration if not provided explicitly.
Here's how you can achieve that, In any of your file in the global context:
import valid8r from '@c4code/valid8r';import valid8r from '@c4code/valid8r';Call defaults function in the valid8r and pass an object of all the fields as the first argument that you want to configure:
valid8r.defaults({
name: {
fullNameWithMiddle: true,
allowNumbers: true,
},
email: {
allowedDomains: ['gmail.com', 'outlook.com'],
allowDisposables: false,
customDisposables: ['xyz.com', 'dispmail.com'],
}
}); // defaults to all the validation in the projectvalid8r.defaults({
name: {
fullNameWithMiddle: true,
allowNumbers: true,
},
email: {
allowedDomains: ['gmail.com', 'outlook.com'],
allowDisposables: false,
customDisposables: ['xyz.com', 'dispmail.com'],
}
}); // defaults to all the validation in the projectThen import the config file to any of the main files for this to work.
import './config.ts';That's it! Now all the validation functions used in the project will use this as their default configuration, until they are specifically provided with their own configuration. Such as:
import valid8r from '@c4code/valid8r';
// Your input's value
const input: string = "Jonathan Wilson2";
const [isValid, errors] = valid8r.name(input);
console.log(isValid); // true, as allowNumbers is set to true.
const input2: string = "D4nny Wils0n";
valid8r.name(input, { allowNumbers: false });
// throws NameValidationFailed errorimport valid8r from '@c4code/valid8r';
// Your input's value
const input = "Jonathan Wilson2";
const [isValid, errors] = valid8r.name(input);
console.log(isValid); // true, as allowNumbers is set to true.
const input2 = "D4nny Wils0n";
valid8r.name(input, { allowNumbers: false });
// throws NameValidationFailed errorThe configuration is flexible and can be customized according to the projects need.
Error Messages
If you don't like the default error messages, and want to configure the error messages according to you valid8r provides a simple way to configure every error message efficiently.
import valid8r from '@c4code/valid8r';import valid8r from '@bitbybit/valid8r';Call defaults function in the valid8r and pass an object that of all the field's error messages your want to configure as the second argument.
valid8r.defaults({
/* Field configurations */
}, {
name: {
onlyLast: "Only last name is allowed!",
noSpChars: "Please dont input any special characters!",
},
}); // defaults to all the validation in the projectvalid8r.defaults({
/* Field configurations */
}, {
name: {
onlyLast: "Only last name is allowed!",
noSpChars: "Please dont input any special characters!",
},
}); // defaults to all the validation in the projectHowever, some fields accept dynamic values that get converts to their relative values when the error is thrown,
import valid8r from '@bitbybit/valid8r';
valid8r.defaults({
/* Field configurations */
}, {
name: {
minLen: "Name should atleast be {minLen} char long!"
},
});
// Your input's value
const input: string = "J";
const [isValid, errors] = valid8r.name(input, {
minLen: 4,
safe: true
});
if (!isValid) {
console.log(errors);
// [{ minLen: "Name should atleast be 4 char long!" }]
}import valid8r from '@bitbybit/valid8r';
// Your input's value
const input = "J";
const [isValid, errors] = valid8r.name(input, { safe: true }, {
minLen: "Name should atleast be {minLen} char long!" // minLen => 3
});
if (!isValid) {
console.log(errors);
// [{ minLen: "Name should atleast be 3 char long!" }]
}You can configure all the field's error message to be used as default in any of the validator
If any validation function requires a specific error you can easily set the error message as the third parameter in any validation function
import valid8r from '@bitbybit/valid8r';
// Your input's value
const input: string = "Jay Carlo$";
const [isValid, errors] = valid8r.name(input, { safe: true }, {
noSpChar: "Special character cannot be included!"
});
if (!isValid) {
console.log(errors);
// [{ noSpChar: "Special character cannot be included!" }]
}import valid8r from '@bitbybit/valid8r';
// Your input's value
const input = "Jay Carlo$";
const [isValid, errors] = valid8r.name(input, { safe: true }, {
noSpChar: "Special character cannot be included!"
});
if (!isValid) {
console.log(errors);
// [{ noSpChar: "Special character cannot be included!" }]
}Last updated