You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

30 lines
953 B
JavaScript

"use strict";
const { validateValue } = require("@validatem/core");
const requireEither = require("./");
let looseRule = requireEither([
"foo",
[ "bar", "baz" ]
], { allowMultiple: true });
let strictRule = requireEither([
"foo",
[ "bar", "baz" ]
]);
console.log(validateValue({ foo: 42 }, looseRule)); // { foo: 42 }
console.log(validateValue({ bar: 42, baz: 14 }, looseRule)); // { bar: 42, baz: 14 }
console.log(validateValue({ foo: true, bar: 42 }, looseRule)); // { foo: true, bar: 42 }
console.log(validateValue({ foo: true, bar: 42 }, strictRule)); /*
AggregrateValidationError: One or more validation errors occurred at 'Object.<anonymous>' in require-either/example.js, at line 22 (<path>):
- Must satisfy at least one of:
├─ bar: Value exists in a place that should be empty
└─ Must satisfy all of the following:
├─ baz: Required value is missing
└─ foo: Value exists in a place that should be empty
*/