UNPKG

@yobta/validator

Version:
13 lines (12 loc) 433 B
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); });