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.
40 lines
1.2 KiB
JavaScript
40 lines
1.2 KiB
JavaScript
"use strict";
|
|
|
|
const { validateValue } = require("@validatem/core");
|
|
const isString = require("@validatem/is-string");
|
|
const wrapError = require("./");
|
|
|
|
/*** Simple rules ***/
|
|
|
|
let rule = wrapError("This is a custom error message complaining that the value must be a string!", [ isString ]);
|
|
|
|
console.log(validateValue("hello world", [ rule ])); // hello world
|
|
|
|
try {
|
|
console.log(validateValue(42, [ rule ]));
|
|
} catch (error) {
|
|
console.log("Error occurred:", error.stack); /*
|
|
Error occurred: AggregrateValidationError: One or more validation errors occurred:
|
|
- At (root): This is a custom error message complaining that the value must be a string!
|
|
*/
|
|
}
|
|
|
|
/*** Combinators with properties, and preserving the original errors as well ***/
|
|
|
|
let objectRule = wrapError("Must be a valid thingem", {
|
|
a: [ isString ],
|
|
b: [ isString ]
|
|
}, { preserveOriginalErrors: true });
|
|
|
|
let objectA = { a: "hello", b: "world" };
|
|
|
|
console.log(validateValue(objectA, [ objectRule ])); // { a: 'hello', b: 'world' }
|
|
|
|
let objectB = { a: "hello", b: 42 };
|
|
|
|
console.log(validateValue(objectB, [ objectRule ])); /*
|
|
AggregrateValidationError: One or more validation errors occurred:
|
|
- At (root): Must be a valid thingem
|
|
- At b: Must be a string
|
|
*/
|