npm install morx
var morx = require('morx');
var spec = morx.spec() //Begin spec-ing parameters
.build('id', 'required:true, map:user_id')
.build('username', 'required:true')
.end(); //End parameter spec-ing
//Call validation
var validated = morx.validate({id:23, username:'demio9009'}, spec);
console.log(validated);
/*
{
no_errors:true,
error_messages:"",
params:{
'user_id':23,
'username':'demio9009'
},
excluded_params:{},
failed_params:[]
}
*/
var morx = require('morx');
var spec = morx.spec() //Begin spec-ing parameters
.build('id', 'required:true, validators:isInt, map:user_id')
.build('username', 'required:true, validators:isAlphanumeric.isAscii, filters:toUpper')
.end(); //End parameter spec-ing
//Call validation
var validated = morx.validate({id:23, username:'demio9009'}, spec);
console.log(validated);
/*
{
no_errors:true,
error_messages:"",
params:{
'user_id':23,
'username':'DEMIO9009'
},
failed_params:[]
}
*/
spec = {
'paramTovalidate':ValidationExtractionAndTransformationRules
}
paramToValidate
- The key / name of the parameter to apply the ObjectValidationRequirements to. E.g. id
, username
e.t.c. If you passing an array as your Parameter source, valid values for paramToValidate
include 0,1 e.t.c.
ValidationExtractionAndTransformationRules
- Key/value pair of a list of rules to use for validating, extracting and transforming paramToValidate
as found in the parameter source. It has the following properties:
required
: (booleanTrue
orFalse
) indicates whether or notparamToValidate
is required. Internally morx checks to see ifparamToValidate
isundefined
or""
map
: If present, indicates the keyparamToValidate
should be returned with. Withoutmap
,id
will be returned asid
. With map (map:user_id
),id
will be returned asuser_id
(see example under basic usage)validators
: A dot separated list of validators to apply toparamToValidate
. Internally, morx uses the validator package on npm to validate parameter values. A list of supported validators can be found here.filters
: A dot separated list of filters / sanitizers to apply toparamToValidate
. All filters with the exception oftoUpper
andtoLower
are the same as those provided by the validator package. A list can be found here.not_param
: A boolean value indicated wether theparamToValidate
should be returned as part of the extracted params. Useful for cases when a parm is required but not needed for functional operations.
Parameter specs can be created either using object literals or morx's inbuilt spec-er:
var paramSpec = morx.spec() //Begin spec-ing parameters
.build('id', 'required:true, map:user_id')
.build('username', 'required:true')
.end(); //End parameter spec-ing
/*
paramSpec is the same as the object literal:
{
id:{
required:true,
map:'user_id'
},
username:{
required:true,
}
}
*/
The morx validator takes three arguments, the first two are required.
var validated = morx.validate(ParameterSource, ParameterSpec, validationOptions)
ParameterSource
- The object / array source to validate the ParameterSpec against i.e The parameters defined in the spec will be looked up in the parameter source. In a function, thearguments
array-like object is a valid parameter source. In a web app / api, the req.body, req.params, req.query objects are all valid parameter sources. (When usingarguments
, an interesting usecase could be to use themap
spec property to transform arguments passed to a function into an object)ParameterSpec
- The spec definition with which to validate and extract values from the parameter source. See specing with morx for a more detailed explanation. The spec also precludes needless parameters from filtering into your apps. In cases of APIs / web projects where req.body / req.query could contain a number of properties, spec-ing ensures only what's needed by a function / service / endpoint is extractedValidationOptions
- The options that define the way morx handles validation success or faliure. By default, morx will not fail until all parameters defined in the spec requirements have been checked - even if one of the requirements of a spec is not met. It will return all failed params as well as error messages. WithValidationOptions
we can fine-tune this behavior. There are three main properties:fail_on_first_error
- If this is set to true, morx will fail (i.e return to the calling function) at the first occurence of a validation errorthrow_error
- If this is set to true, instead of returning a well formed object, morx will throw an error with the validation error messagesthrow_error_on_first_fail
- Similar tofail_on_first_error
but instead of returning, an error with the validation error message is thrown.
Auto-test generation with morx-cha
To use morx with morx-cha, the additional spec properties are required
eg
- Example value for the parametereg_specialcase
- Example special case value for the parameter, e.g. eg_invalid_email or eg_exists e.t.c. This is yet to be implemented
See example here