1
1
mirror of https://github.com/namibia/awesome-cheatsheets.git synced 2024-12-22 10:08:54 +00:00

docs(adonis): add validation rules

This commit is contained in:
Julien Le Coupanec 2020-04-12 17:05:46 +02:00
parent b310c4b285
commit a3e8091ca2

View File

@ -77,9 +77,71 @@ Route.group(() => {}).domain('blog.sthg.com') // Specify which domain goup rout
* https://indicative-v5.adonisjs.com/
********************************************************************************************/
const indicative = require("indicative");
const rules = {
email: "required|email|unique:users",
password: "required|min:6|max:30",
};
// Indivative methods
indicative.validate(data, rules); // Validate data with defined rules
indicative.validateAll(data, rules); // Same as validate but continues to validate all fields, whereas the validate method stops on first error
indicative.is.email(emailAddress); // Raw validator
indicative.extend("exists", existsFn); // Add your own rules
indicative.sanitize(data, rules); // Returns a new object with sanitized data:
indicative.sanitizor.normalizeEmail(emailAddress); // Raw sanitizor
// Validations
above // Makes sure the value provided by the end user is above the expected value.
accepted // Ensures that the field under validation is accepted.
after // Ensures the value of the field is after the expected date.
afterOffsetOf // Ensures the date is after a given offset of a given time period.
alpha // Makes sure the field under validation is alpha only.
alphaNumeric // Makes sure the field under validation is alpha numeric only.
array // Ensure the value is a valid array. Also this validation will never validate the size of array.
before // Ensures the value of field under validation is before a given date.
beforeOffsetOf // Ensures the date is before a given offset of a given time period.
boolean // Ensures the value of a field is a boolean.
confirmed // Ensures a field value as confirmed using a _confirmation convention. This is mainly used for password confirmation field.
date // Ensures the field under validation is a valid date. The value can be a date object or a valid date string.
dateFormat // Ensures the date or date time is valid as the one of the defined formats.
different // Ensures the value of the field under validation is always different from the targeted field value.
email // Ensures the field under validation is a valid email format.
endsWith // Ensure the value of field under validation ends with a certain substr. This validation will also trim whitespaces before making the check.
equals // Ensures 2 values are lossely same. This validation will not check for the same type, but instead checks for the same value.
in // Ensures the value of a given field matches one of expected values.
includes // Ensures the value of field under validation contains a given substring.
integer // Ensures the value is a valid integer. Also string representation of a number will return true.
ip // Ensures the value is a valid ip address as per ipv4 and ipv6 specs.
ipv4 // Ensures the value is a valid ip address as per ipv4 spec only.
ipv6 // Ensures the value is a valid ip address as per ipv6 spec only.
json // Ensures the value of field under validation is safe to be parsed using JSON.parse method.
max // Ensures the length of a string or array is not greater than the defined length.
min // Ensures the length of a string or array is not is not less than the expected length
notEquals // Makes sure that the value of field under validation is not same as the defined value.
notIn // Makes sure that the value of field under validation is not from one of the defined values.
number // Makes sure that the value of field under validation is a valid number. The validation will pass for floats too, since it uses typeof internally.
object // Ensures the value of field under validation is a valid Javascript object. The validation will fail for Arrays, though they are objects too in Javascript.
range // Ensures the value of field under validation is under a given range. The values will be cased to Number automatically.
regex // Ensures the value of field under validation, passes the regex test. The regex can be defined as a string or a RegExp object.
required // Ensures the value of field under validation is not empty (i.e. not an empty object, empty array, empty string, null or undefined).
requiredIf // The field is checked for required validation, when expected field exists.
requiredWhen // The field is checked for required validation, when expected field value is same as the expected value.
requiredWithAll // Ensures the field is required when all other fields have non-empty values.
requiredWithAny // Ensures the field is required when any of the other fields have non-empty values.
requiredWithoutAll // Ensures the field is required when all of the other fields has empty values.
requiredWithoutAny // Ensures the field is required when any of the other fields has empty values.
same // Ensures the value of 2 fields are same.
startsWith // Ensure the value of field under validation starts with a certain substr. This validation will also trim whitespaces before making the check.
string // Ensures the value is a string.
under // Ensures the value of a field is under a certain value. All values will be casted to Number.
url // Ensures the value is a valid URL format.
/********************************************************************************************
* HOOKS
* DATABASE HOOKS
* https://adonisjs.com/docs/4.1/database-hooks
********************************************************************************************/