Skip to content

Instantly share code, notes, and snippets.

@floatdrop
Created June 24, 2014 03:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save floatdrop/f5a4d8bd8eeea3c8aca2 to your computer and use it in GitHub Desktop.
Save floatdrop/f5a4d8bd8eeea3c8aca2 to your computer and use it in GitHub Desktop.
xml2js validation
'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