UNPKG

request-validator

Version:

Schema-based request validation middleware for express and connect.

28 lines (24 loc) 610 B
'use strict'; module.exports = function validateAnyOf(schema, value) { var that = this, errors = [], schemas = schema.anyOf, matchFound = false; schemas.forEach(function forEachSchema(childSchema) { if (matchFound) { // short-circuit any further validation // if there is a match already return; } try { that.validate(childSchema, value); matchFound = true; } catch (e) { errors.push(e); } }); if (!matchFound) { throw errors[0]; } };