@yobta/validator
Version:
Tree-shakable es6 validator
13 lines (12 loc) • 433 B
JavaScript
import { rule } from '../rule/rule.js';
const coercedTypes = new Set(['boolean', 'number', 'string']);
export const stringMessage = 'It should be a string';
export const string = (message = stringMessage) => rule((value = '') => {
if (value === '') {
return undefined;
}
if (value instanceof String || coercedTypes.has(typeof value)) {
return String(value).trim();
}
throw new Error(message);
});