@smallwins/validate
Advanced tools
Comparing version 3.0.0 to 3.1.0
var validate = require('../') | ||
var ISO = require('../iso') | ||
var Email = require('../email') | ||
var UUID = require('../uuid') | ||
/** | ||
* custom types are functions that accept a value and | ||
* return either an Error or true | ||
*/ | ||
function ISO(v) { | ||
var exp = new RegExp(/(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d\.\d+([+-][0-2]\d:[0-5]\d|Z))|(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d([+-][0-2]\d:[0-5]\d|Z))|(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d([+-][0-2]\d:[0-5]\d|Z))/) | ||
return exp.test(v)? true : Error('not an ISO date string') | ||
} | ||
function Email(v) { | ||
var exp = new RegExp(/.+\@.+\..+/) | ||
return exp.test(v)? true : Error('not a valid email address') | ||
} | ||
module.exports = function customs(params, callback) { | ||
@@ -22,3 +10,4 @@ // define our assumed params | ||
'created': {required:true, type:ISO}, | ||
'email': {required:true, type:Email} | ||
'email': {required:true, type:Email}, | ||
'uuid': {required:true, type:UUID} | ||
}) | ||
@@ -25,0 +14,0 @@ if (errors) { |
var validate = require('../') | ||
function hi(params, callback) { | ||
var schema = { | ||
'name': {required:true, type:Object}, | ||
'name.first': {required:true, type:String}, | ||
'name.last': {required:false, type:String} | ||
} | ||
var errors = validate(params, schema) | ||
if (errors) { | ||
callback(errors) | ||
} | ||
else { | ||
callback(null, 'hi ' + params.first) | ||
} | ||
var schema = { | ||
'name': {required:true, type:Object}, | ||
'name.first': {required:true, type:String}, | ||
'name.last': {required:false, type:String} | ||
} | ||
var errors = validate(params, schema) | ||
if (errors) { | ||
callback(errors) | ||
} | ||
else { | ||
callback(null, 'hi ' + params.first) | ||
} | ||
} | ||
// logs: null, hi brian | ||
hi({name:{first:'brian', last:'leroux'}}, console.log) | ||
var goodParams = { | ||
name: { | ||
first: 'brian', | ||
last: 'leroux' | ||
} | ||
} | ||
hi(goodParams, console.log) | ||
// logs: [ [ReferenceError: missing required param name.first] ] | ||
hi({name:{}}, console.log) | ||
var badParams = {name:{}} | ||
hi(badParams, console.log) |
var validate = require('../') | ||
var DateRange = require('../daterange') | ||
function DateRange(v) { | ||
// expects a string 'YYYY/MM/DD-YYYY/MM/DD' | ||
var exp = /(\d+\/\d{2}\/\d{2})-(\d+\/\d{2}\/\d{2})/ | ||
// implement min and max on the type | ||
DateRange.min = function min(min, actual) { | ||
var matches = actual.match(exp) | ||
var start = matches[1] // get the first date | ||
min = new Date(min) | ||
start = new Date(start) | ||
return min <= start | ||
} | ||
DateRange.max = function max(max, actual) { | ||
var matches = actual.match(exp) | ||
var end = matches[2] // get the second date | ||
max = new Date(max) | ||
end = new Date(end) | ||
return max >= end | ||
} | ||
return exp.test(v)? true : Error('not a valid DateRange') | ||
} | ||
module.exports = function customs(params, callback) { | ||
@@ -29,0 +5,0 @@ // define our assumed params |
@@ -11,2 +11,4 @@ var isObject = require('lodash.isobject') | ||
var property = require('lodash.property') | ||
// data structures | ||
var aliases = 'obj str num arr bool fun'.split(' ') | ||
@@ -91,3 +93,3 @@ var builtins = [Object, String, Number, Array, Boolean, Function] | ||
if (isError(err)) { | ||
errors.push(TypeError('invalid type ' + k + ' is ' + err.message)) | ||
errors.push(TypeError('invalid type ' + k + ' is an ' + err.message)) | ||
} | ||
@@ -94,0 +96,0 @@ } |
{ | ||
"name": "@smallwins/validate", | ||
"version": "3.0.0", | ||
"version": "3.1.0", | ||
"main": "index", | ||
@@ -19,2 +19,3 @@ "scripts": { | ||
"dependencies": { | ||
"is-uuid": "^1.0.2", | ||
"lodash.has": "^4.2.1", | ||
@@ -21,0 +22,0 @@ "lodash.isarray": "^4.0.0", |
@@ -91,2 +91,39 @@ [ ![Codeship Status for smallwins/validate](https://codeship.com/projects/e0e990b0-d826-0133-2fa3-6a1daaefbd5c/status?branch=master)](https://codeship.com/projects/143153) | ||
## bundled custom types | ||
- `UUID` | ||
- `Email` | ||
- `ISO` | ||
- `DateRange` | ||
Example usage of custom types: | ||
```javascript | ||
var validate = require('@smallwins/validate') | ||
var lambda = require('@smallwins/lambda') | ||
// pull in the custom types | ||
var UUID = require('@smallwins/validate/uuid') | ||
var Email = require('@smallwins/validate/email') | ||
var ISO = require('@smallwins/validate/iso') | ||
var DateRange = require('@smallwins/validate/daterange') | ||
// use the schema per builtins | ||
function valid(event, callback) { | ||
var schema = { | ||
'params.id': {required:true, type:UUID}, | ||
'body.email': {required:true, type:Email}, | ||
'body.created': {required:true, type:ISO}, | ||
'body.duration': {required:true, type:DateRange, min:'2016/01/01', max:'2017/01/01'} | ||
} | ||
validate(event, schema, callback) | ||
} | ||
function save(event, callback) { | ||
// performs save | ||
callback(null, event) | ||
} | ||
exports.handler = lambda(valid, save) | ||
``` | ||
Check out the [examples](https://github.com/smallwins/validate-params-schema/tree/master/examples) for more on custom types and ranges (and the tests). |
var test = require('tape') | ||
var customs = require('../examples/customs') | ||
test('basics', t=> { | ||
test('customs', t=> { | ||
t.plan(1) | ||
@@ -14,7 +14,6 @@ customs({}, (err, results)=> { | ||
t.plan(1) | ||
customs({created:'tues', email:'brian.io'}, (err, results)=> { | ||
t.equal(err.length, 2, 'got errors for empty params') | ||
customs({created:'tues', email:'brian.io', uuid:'x'}, (err, results)=> { | ||
t.equal(err.length, 3, 'got errors for empty params') | ||
console.log(err) | ||
}) | ||
}) | ||
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
15777
18
361
129
12
+ Addedis-uuid@^1.0.2
+ Addedis-uuid@1.0.2(transitive)