import {ValidationResult, ValidatorFunction} from "./types"; import {UniversalPath} from '../../util' export namespace Is { /** Requires the given input value to be some form of affirmative boolean. */ export function accepted(fieldName: string, inputValue: any): ValidationResult { if ( ['yes', 'Yes', 'YES', 1, true, 'true', 'True', 'TRUE'].includes(inputValue) ) { return { valid: true } } return { valid: false, message: 'must be accepted' } } /** Requires the given input value to be some form of boolean. */ export function boolean(fieldName: string, inputValue: any): ValidationResult { const boolish = ['true', 'True', 'TRUE', '1', 'false', 'False', 'FALSE', '0', true, false, 1, 0] if ( boolish.includes(inputValue) ) { return { valid: true } } return { valid: false, message: 'must be true or false' } } /** Requires the input value to be of type string. */ export function string(fieldName: string, inputValue: any): ValidationResult { if ( typeof inputValue === 'string' ) { return { valid: true } } return { valid: false, message: 'must be a string' } } /** Requires the given input value to be present and non-nullish. */ export function required(fieldName: string, inputValue: any): ValidationResult { if ( typeof inputValue !== 'undefined' && inputValue !== null && inputValue !== '' ) { return { valid: true } } return { valid: false, message: 'is required', stopValidation: true, } } /** Alias of required(). */ export function present(fieldName: string, inputValue: any): ValidationResult { return required(fieldName, inputValue) } /** Alias of required(). */ export function filled(fieldName: string, inputValue: any): ValidationResult { return required(fieldName, inputValue) } /** Requires the given input value to be absent or nullish. */ export function prohibited(fieldName: string, inputValue: any): ValidationResult { if ( typeof inputValue === 'undefined' || inputValue === null || inputValue === '' ) { return { valid: true } } return { valid: false, message: 'is not allowed', stopValidation: true, } } /** Alias of prohibited(). */ export function absent(fieldName: string, inputValue: any): ValidationResult { return prohibited(fieldName, inputValue) } /** Alias of prohibited(). */ export function empty(fieldName: string, inputValue: any): ValidationResult { return prohibited(fieldName, inputValue) } /** * Builds a validator function that requires the given input to be found in an array of values. * @param values */ export function foundIn(values: any[]): ValidatorFunction { return function foundIn(fieldName: string, inputValue: any): ValidationResult { if ( values.includes(inputValue) ) { return { valid: true } } return { valid: false, message: `must be one of: ${values.join(', ')}` } } } /** * Builds a validator function that requires the given input NOT to be found in an array of values. * @param values */ export function notFoundIn(values: any[]): ValidatorFunction { return function foundIn(fieldName: string, inputValue: any): ValidationResult { if ( values.includes(inputValue) ) { return { valid: true } } return { valid: false, message: `must be one of: ${values.join(', ')}` } } } /** Requires the input value to be number-like. */ export function numeric(fieldName: string, inputValue: any): ValidationResult { if ( !isNaN(parseFloat(inputValue)) ) { return { valid: true } } return { valid: false, message: 'must be numeric', } } /** Requires the given input value to be integer-like. */ export function integer(fieldName: string, inputValue: any): ValidationResult { if ( !isNaN(parseInt(inputValue)) && parseInt(inputValue) === parseFloat(inputValue) ) { return { valid: true } } return { valid: false, message: 'must be an integer', } } /** Requires the given input value to be a UniversalPath. */ export function file(fieldName: string, inputValue: any): ValidationResult { if ( inputValue instanceof UniversalPath ) { return { valid: true } } return { valid: false, message: 'must be a file' } } /** * A special validator function that marks a field as optional. * If the value of the field is nullish, no further validation rules will be applied. * If it is non-nullish, validation will continue. * @param fieldName * @param inputValue */ export function optional(fieldName: string, inputValue: any): ValidationResult { if ( inputValue ?? true ) { return { valid: true, stopValidation: true, } } return { valid: true } } }