examiner
Validate your data however you want
Installation
npm install --save examiner
Variable filters
; var user = hasMessages: true messages: body: 'message 1' body: 'message 2' body: 'message 3' ; var validator = rules: { return datahasMessages ? 'required|min:10|string' : null; } ;validator;
You can use Validator.rules
which works pretty like classNames module, and you can use like this for toggle specific filters in a rule while using the variable filters feature:
; // or just Validator.rules var validator = rules: { return ; } ;
Using presets
Presets could be useful if you want to reuse validation rules or replaces into more than one Validator instance without need to rewrite anything.
; Validator; var validator = rules: 'name': 'required|string|min:10' presets: 'user_email_validation'; validator;
Extending presets
;; Validator;Validator; var user = deep: name: 'john crockful' ; var validator = presets: { return rules: _; ; } { return false; } ;
The preset another_cool_preset
will be ignored for now since it returned as false, and my_custom_preset
will be extended to the following. But only internally at the time of the validation, the global preset will stay the same for other Validator instances:
rules: { return datashouldRequiredName ? 'required|string' : null; }
There are still some helper functions at the constructor of Validator
, like Validator.transformPreset
which can be used to add a prefix in front of the preset keys
and replaces
:
Validator; var validator = presets: { return Validator; } ;
Validating arrays
What really happens here is when you have a expression that contain a $ between two dots like user.messages.$.name
it searchs into the given data and generate temporary rules
and replaces
according to the array of your data.
; var user = documents: name: 'My id 1' name: 'My id 2' ;var validator = rules: 'documents.$.name': 'required|min:10' replaces: // the messages will be like: // - The field "document name 4" is required // - The field "document name 4" need to have at least 10 characters { return `document name `; } ;validator;
Internally the validator has been created the following rules
and replaces
to validate your data:
rules: 'documents.0.name': 'required|min:10' 'documents.1.name': 'required|min:10' replaces: 'documents.0.name': 'document name 1' 'documents.1.name': 'document name 2'
While using $
on expressions, all the rules are generated based on the data
passed to validator.validate
function. So when searching for deep keys, you cannot provide undefined
properties. The following example WILL NOT WORK:
var validator = rules: 'phonenumbers.$': 'required|string|size:9' ;validator;
The correct way to call validator.validate
is:
validator.validate({
phonenumbers: new Array(10)
});
Let's not forgot that when you're creating a dot expression with a $
, your replace
for that expression can and should carry a function which will be executed and the first argument will always be the index
of the current array item. And before you can ask, there is no limit for how many $
you can put in a simple dot expression.
Translating messages
First you should make a copy of lang/en/messages.js, change, and import the new one in your Validator instance:
; var validator = ;validator;validator; // GloballyValidator;
Using with ReactJS
import _ from 'lodash';import React from 'react';import Validator from 'examiner'; var AppIndex = React;
Private filters
var Validator = Validator;var validator = rules: ;validator;
Global filters
var Validator = Validator;Validator;
Need more? Come and check more examples!