strngle
v0.1.6
Published
Composable string validation!
Downloads
13
Maintainers
Readme
Strngle
Composable string validation!
A pretty neat way to validate the content of strings. Use the validators Strngle comes with, or add your own. Strngle is written in Typescript and comes with full type definitions.
Docs will be added eventually.
Installation
npm:
npm i strngleyarn:
yarn add strngleBasic usage
import { validate, required, hasMinMax } from 'strngle';
const { valid, message } = validate(someUnknownValue, [
required(),
hasMinMax({ min: 5, max: 10 }),
]);
if (!valid) {
console.error(`The input is invalid: ${message}!`);
}Adding your own validators
import { validate } from 'strngl';
const startsWith = (character) => {
return {
validator(value) {
if (value.charAt(0) !== character) {
return {
valid: false,
message: `Must begin with ${character}`,
};
}
return {
valid: true,
};
},
};
};
const { valid } = validate(someString, [startsWith('A')]);