Created
June 24, 2014 03:41
-
-
Save floatdrop/f5a4d8bd8eeea3c8aca2 to your computer and use it in GitHub Desktop.
xml2js validation
This file contains 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'); | |
function attrs (keys) { | |
return Joi.object().keys({ | |
__attr: Joi.compile(keys).required() | |
}); | |
} | |
function text (assertion) { | |
assertion = assertion || Joi.string(); | |
return Joi.alternatives().try( | |
Joi.array().length(1).includes(assertion), | |
Joi.object().keys({'__text': assertion}) | |
); | |
} | |
function textAttr (textAssertion, attrsAssertion) { | |
return Joi.object().keys({ | |
__attr: attrsAssertion, | |
__text: textAssertion | |
}); | |
} | |
function tags (length, assertion) { | |
var joi = Joi.array(); | |
if (length) { | |
joi = joi.length(length); | |
} | |
if (typeof assertion === 'object' && !assertion.isJoi) { | |
return joi.includes(Joi.object().keys(assertion)); | |
} | |
return joi.includes(assertion); | |
} | |
module.exports = { | |
attrs: attrs, | |
text: text, | |
tags: tags.bind(null, null), | |
tag: tags.bind(null, 1), | |
textAttr: textAttr | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment