Skip to content

Instantly share code, notes, and snippets.

@dstevensio
Created December 3, 2014 22:25
Show Gist options
  • Save dstevensio/cd29467a5a9fa9c45667 to your computer and use it in GitHub Desktop.
Save dstevensio/cd29467a5a9fa9c45667 to your computer and use it in GitHub Desktop.
joi issue potential solution
"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