1.2.0 • Published 6 years ago
recipe-consumer v1.2.0
recipe-consumer-js
Consume your json & javascript recipe.
Install
npm install --save recipe-consumer-js
Example
const recipeConsumer = require('recipe-consumer-js')
const recipe = require('./path-to-my-recipe.json')
const dataset = {...}
const validation = recipeConsumer.consumer(recipe, data)
if (validation.status === 'invalid') {
return validation.errors
}
You can see a complete example from /tests/lib/index.js
Usage
RecipeConsumer.consume(recipe, data)
It used to consumer a recipe. It take two parameters, recipe & data.
recipe
- The list of rules to validate data (object).data
- The dataset (object).
It returns an object with the status property.
When it return an error, it's include property error
.
RecipeConsumer.seedCustomsMessages(messagesMap)
messagesMap
- The messages map (object).
You can define custom error messages by using a map recipe before consuming your recipes. The recipe can include as many definitions you need.
The definition message can use multiple parameters :
key
- Current item. (string)type
- Current type. (string)params
- Parameters functions. (array)
To use customs messages you should transform basic rule parameters to array. You can mix basic & customs errors messages.
Basic
{
fields: {
username: {
required: true,
type: "string",
rules: {
minlength: 4
}
}
}
}
Customs
{
fields: {
username: {
required: [true, "IS_REQUIRED"],
type: ["string", "STRING_TYPE"],
rules: {
minlength: [4, "STRING_MIN_LENGTH"]
}
}
}
}
Available recipe
Each recipe object can contain two out-of-rule parameters (type & required).
Types
- Array
- Boolean
- Float
- Integer
- Object
- String
Array
minlength(length)
- Min length.maxlength(length)
- Max length.length(length)
- Strict length.hasvalue(value)
- Include value.equal(value)
- Equal to value.diff(value)
- Diff to value.
Boolean
isTrue(confirmation)
- Is true.isFalse(confirmation)
- Is false.
Float
min(value)
- Min value.max(value)
- Max value.equal(value)
- Equal to value.diff(value)
- Diff to value.positive(confirmation)
- Is positive.negative(confirmation)
- Is negative.zero(confirmation)
- Is zero.mindecimal(value)
- Min nbr of decimals.maxdecimal(value)
- Max nbr of decimals.decimal(value)
- Nbr of decimals.
Integer
min(value)
- Min value.max(value)
- Max value.equal(value)
- Equal to value.diff(value)
- Diff to value.positive(confirmation)
- Is positive.negative(confirmation)
- Is negative.zero(confirmation)
- Is zero.precision({ limit, range })
- It's on range.
Object
hasProperty(key)
- Has property.hasKeyValue({ key, value })
- Has property with value.equal(value)
- Equal to value.diff(value)
- Diff to value.
String
minlength(length)
- Min length.maxlength(length)
- Max length.length(length)
- Strict length.match(value)
- Match to value.diff(value)
- Diff to value.regex(format)
- Has a specific format.
Allowed regex format (usable from stringValidation only) :
email
url
hostname
ip
ipv4
ipv6
More
You can see complete recipe examples from test directory.