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.

35 lines
872 B
JavaScript

"use strict";
const nestedStructureOf = require("./");
const { validateValue } = require("@validatem/core");
const isString = require("@validatem/is-string");
const isNumber = require("@validatem/is-number");
const either = require("@validatem/either");
let validator = nestedStructureOf(either([ isString, isNumber ]));
let arrayA = [
"hello world",
[ "foo", 42 ],
"baz",
{ qux: "quz" }
];
console.log(validateValue(arrayA, validator)); // [ 'hello world', [ 'foo', 42 ], 'baz', { qux: 'quz' } ]
let arrayB = [
"hello world",
[ "foo", NaN ],
"baz",
{ qux: "quz" }
];
console.log(validateValue(arrayB, validator)); /*
AggregrateValidationError: One or more validation errors occurred:
- At 1 -> 1: Must satisfy at least one of:
├─ Must be a string
├─ Must be a number (must not be NaN)
├─ Must be an array
└─ Must be an object
*/