isStringLength
isStringLength({ min, max })Verifies if provided Verificable represents a string with length within specified range. Both min and max arguments are optional - not providing any of them means value on that 'end' will not be checked.
Arguments:
min- optional, of typenumbermax- optional, of typenumber
Possible errors:
If provided Verificable is not a string:
Verificable is not a string:{
type: "isString",
path: [/* ... */]
}If provided Verificable is a string with length being outside of the range:
Verificable is a string with length being outside of the range:{
type: "isStringLength",
path: [/* ... */],
min: /* provided min value */,
max: /* provided max value */,
}Examples:
Each of examples uses this import statement:
const { asVerificable, isValid, isStringLength, getErrors } = require("verifica");Non-string values will fail with an isString error:
isString error:const verificable = asVerificable({ prop: "value" });
isValid(verificable, isStringLength({ min: 2, max: 4 })); // false
getErrors(verificable, isStringLength({ min: 2, max: 4 }));
// [ { type: "isString", path: [] }]String values with length outside of requested range will fail with isStringLength error:
isStringLength error:const verificable = asVerificable("Quite a long string");
isValid(verificable, isStringLength({ min: 2, max: 4 })); // false
getErrors(verificable, isStringLength({ min: 2, max: 4 }));
// [ { type: "isStringLength", path: [], min: 2, max: 4 }]String values with length inside of requested range will match:
const verificable = asVerificable("abc");
isValid(verificable, isStringLength({ min: 2, max: 4 })); // trueNot providing min, max or both constrains will mean no requirement on those missing 'ends' of the range:
min, max or both constrains will mean no requirement on those missing 'ends' of the range:const verificable = asVerificable("Quite a long string");
isValid(verificable, isStringLength({ min: 2 })); // trueLast updated
Was this helpful?