Created
December 3, 2014 22:25
-
-
Save dstevensio/cd29467a5a9fa9c45667 to your computer and use it in GitHub Desktop.
joi issue potential solution
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
var joi = require("joi"); | |
var data = [{ | |
type : "A", | |
ip : "123.123.123.123" | |
}, | |
{ | |
type: "A", | |
ip: "1AB.123.233.222" // To show one that fails - remove once you've verified | |
}, | |
{ | |
type : "A", | |
ip : [ | |
"123.123.123.123", | |
"123.123.123.123" | |
] | |
}, { | |
type : "CNAME", | |
ip : "foogawooga" | |
}]; | |
var ipTest = joi.string().regex(/(?:\d{3}\.){3}\d{3}/), | |
schema = joi.array().includes({ | |
type : joi.string().valid([ "A", "CNAME" ]), | |
ip : [ | |
joi.string().min(1).when("type", { is: "A", then : ipTest }), | |
joi.array().single().includes( | |
joi.string().min(1) | |
.when("type", { is : "A", then : ipTest }) | |
)] | |
}); | |
joi.validate(data, schema, { abortEarly : false }, function(err) { | |
console.log(require("util").inspect(err, { depth : null })); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment