2021-06-03 03:36:25 +00:00
|
|
|
import {ValidationResult, ValidatorFunction} from './types'
|
2021-06-02 01:59:40 +00:00
|
|
|
import {UniversalPath} from '../../util'
|
|
|
|
|
2021-06-03 03:36:25 +00:00
|
|
|
/** Requires the given input value to be some form of affirmative boolean. */
|
|
|
|
function accepted(fieldName: string, inputValue: unknown): ValidationResult {
|
|
|
|
if ( ['yes', 'Yes', 'YES', 1, true, 'true', 'True', 'TRUE'].includes(String(inputValue)) ) {
|
|
|
|
return { valid: true }
|
2021-06-02 01:59:40 +00:00
|
|
|
}
|
|
|
|
|
2021-06-03 03:36:25 +00:00
|
|
|
return {
|
|
|
|
valid: false,
|
|
|
|
message: 'must be accepted',
|
2021-06-02 01:59:40 +00:00
|
|
|
}
|
2021-06-03 03:36:25 +00:00
|
|
|
}
|
2021-06-02 01:59:40 +00:00
|
|
|
|
2021-06-03 03:36:25 +00:00
|
|
|
/** Requires the given input value to be some form of boolean. */
|
|
|
|
function boolean(fieldName: string, inputValue: unknown): ValidationResult {
|
|
|
|
const boolish = ['true', 'True', 'TRUE', '1', 'false', 'False', 'FALSE', '0', true, false, 1, 0]
|
|
|
|
if ( boolish.includes(String(inputValue)) ) {
|
|
|
|
return { valid: true }
|
2021-06-02 01:59:40 +00:00
|
|
|
}
|
|
|
|
|
2021-06-03 03:36:25 +00:00
|
|
|
return {
|
|
|
|
valid: false,
|
|
|
|
message: 'must be true or false',
|
2021-06-02 01:59:40 +00:00
|
|
|
}
|
2021-06-03 03:36:25 +00:00
|
|
|
}
|
2021-06-02 01:59:40 +00:00
|
|
|
|
2021-06-03 03:36:25 +00:00
|
|
|
/** Requires the input value to be of type string. */
|
|
|
|
function string(fieldName: string, inputValue: unknown): ValidationResult {
|
|
|
|
if ( typeof inputValue === 'string' ) {
|
|
|
|
return { valid: true }
|
2021-06-02 01:59:40 +00:00
|
|
|
}
|
|
|
|
|
2021-06-03 03:36:25 +00:00
|
|
|
return {
|
|
|
|
valid: false,
|
|
|
|
message: 'must be a string',
|
2021-06-02 01:59:40 +00:00
|
|
|
}
|
2021-06-03 03:36:25 +00:00
|
|
|
}
|
2021-06-02 01:59:40 +00:00
|
|
|
|
2021-06-03 03:36:25 +00:00
|
|
|
/** Requires the given input value to be present and non-nullish. */
|
|
|
|
function required(fieldName: string, inputValue: unknown): ValidationResult {
|
|
|
|
if ( typeof inputValue !== 'undefined' && inputValue !== null && inputValue !== '' ) {
|
|
|
|
return { valid: true }
|
2021-06-02 01:59:40 +00:00
|
|
|
}
|
|
|
|
|
2021-06-03 03:36:25 +00:00
|
|
|
return {
|
|
|
|
valid: false,
|
|
|
|
message: 'is required',
|
|
|
|
stopValidation: true,
|
2021-06-02 01:59:40 +00:00
|
|
|
}
|
2021-06-03 03:36:25 +00:00
|
|
|
}
|
2021-06-02 01:59:40 +00:00
|
|
|
|
2021-06-03 03:36:25 +00:00
|
|
|
/** Alias of required(). */
|
|
|
|
function present(fieldName: string, inputValue: unknown): ValidationResult {
|
|
|
|
return required(fieldName, inputValue)
|
|
|
|
}
|
2021-06-02 01:59:40 +00:00
|
|
|
|
2021-06-03 03:36:25 +00:00
|
|
|
/** Alias of required(). */
|
|
|
|
function filled(fieldName: string, inputValue: unknown): ValidationResult {
|
|
|
|
return required(fieldName, inputValue)
|
|
|
|
}
|
2021-06-02 01:59:40 +00:00
|
|
|
|
2021-06-03 03:36:25 +00:00
|
|
|
/** Requires the given input value to be absent or nullish. */
|
|
|
|
function prohibited(fieldName: string, inputValue: unknown): ValidationResult {
|
|
|
|
if ( typeof inputValue === 'undefined' || inputValue === null || inputValue === '' ) {
|
|
|
|
return { valid: true }
|
2021-06-02 01:59:40 +00:00
|
|
|
}
|
|
|
|
|
2021-06-03 03:36:25 +00:00
|
|
|
return {
|
|
|
|
valid: false,
|
|
|
|
message: 'is not allowed',
|
|
|
|
stopValidation: true,
|
2021-06-02 01:59:40 +00:00
|
|
|
}
|
2021-06-03 03:36:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Alias of prohibited(). */
|
|
|
|
function absent(fieldName: string, inputValue: unknown): ValidationResult {
|
|
|
|
return prohibited(fieldName, inputValue)
|
|
|
|
}
|
2021-06-02 01:59:40 +00:00
|
|
|
|
2021-06-03 03:36:25 +00:00
|
|
|
/** Alias of prohibited(). */
|
|
|
|
function empty(fieldName: string, inputValue: unknown): ValidationResult {
|
|
|
|
return prohibited(fieldName, inputValue)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Builds a validator function that requires the given input to be found in an array of values.
|
|
|
|
* @param values
|
|
|
|
*/
|
|
|
|
function foundIn(values: any[]): ValidatorFunction {
|
|
|
|
return (fieldName: string, inputValue: unknown): ValidationResult => {
|
|
|
|
if ( values.includes(inputValue) ) {
|
2021-06-02 01:59:40 +00:00
|
|
|
return { valid: true }
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
valid: false,
|
2021-06-03 03:36:25 +00:00
|
|
|
message: `must be one of: ${values.join(', ')}`,
|
2021-06-02 01:59:40 +00:00
|
|
|
}
|
|
|
|
}
|
2021-06-03 03:36:25 +00:00
|
|
|
}
|
2021-06-02 01:59:40 +00:00
|
|
|
|
2021-06-03 03:36:25 +00:00
|
|
|
/**
|
|
|
|
* Builds a validator function that requires the given input NOT to be found in an array of values.
|
|
|
|
* @param values
|
|
|
|
*/
|
|
|
|
function notFoundIn(values: any[]): ValidatorFunction {
|
|
|
|
return (fieldName: string, inputValue: unknown): ValidationResult => {
|
|
|
|
if ( values.includes(inputValue) ) {
|
2021-06-02 01:59:40 +00:00
|
|
|
return { valid: true }
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
valid: false,
|
2021-06-03 03:36:25 +00:00
|
|
|
message: `must be one of: ${values.join(', ')}`,
|
2021-06-02 01:59:40 +00:00
|
|
|
}
|
|
|
|
}
|
2021-06-03 03:36:25 +00:00
|
|
|
}
|
2021-06-02 01:59:40 +00:00
|
|
|
|
2021-06-03 03:36:25 +00:00
|
|
|
/** Requires the input value to be number-like. */
|
|
|
|
function numeric(fieldName: string, inputValue: unknown): ValidationResult {
|
|
|
|
if ( !isNaN(parseFloat(String(inputValue))) ) {
|
|
|
|
return { valid: true }
|
|
|
|
}
|
2021-06-02 01:59:40 +00:00
|
|
|
|
2021-06-03 03:36:25 +00:00
|
|
|
return {
|
|
|
|
valid: false,
|
|
|
|
message: 'must be numeric',
|
2021-06-02 01:59:40 +00:00
|
|
|
}
|
2021-06-03 03:36:25 +00:00
|
|
|
}
|
2021-06-02 01:59:40 +00:00
|
|
|
|
2021-06-03 03:36:25 +00:00
|
|
|
/** Requires the given input value to be integer-like. */
|
|
|
|
function integer(fieldName: string, inputValue: unknown): ValidationResult {
|
|
|
|
if ( !isNaN(parseInt(String(inputValue), 10)) && parseInt(String(inputValue), 10) === parseFloat(String(inputValue)) ) {
|
|
|
|
return { valid: true }
|
|
|
|
}
|
2021-06-02 01:59:40 +00:00
|
|
|
|
2021-06-03 03:36:25 +00:00
|
|
|
return {
|
|
|
|
valid: false,
|
|
|
|
message: 'must be an integer',
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Requires the given input value to be a UniversalPath. */
|
|
|
|
function file(fieldName: string, inputValue: unknown): ValidationResult {
|
|
|
|
if ( inputValue instanceof UniversalPath ) {
|
2021-06-02 01:59:40 +00:00
|
|
|
return { valid: true }
|
|
|
|
}
|
2021-06-03 03:36:25 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
function optional(fieldName: string, inputValue: unknown): ValidationResult {
|
|
|
|
if ( inputValue ?? true ) {
|
|
|
|
return {
|
|
|
|
valid: true,
|
|
|
|
stopValidation: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return { valid: true }
|
|
|
|
}
|
|
|
|
|
|
|
|
export const Is = {
|
|
|
|
accepted,
|
|
|
|
boolean,
|
|
|
|
string,
|
|
|
|
required,
|
|
|
|
present,
|
|
|
|
filled,
|
|
|
|
prohibited,
|
|
|
|
absent,
|
|
|
|
empty,
|
|
|
|
foundIn,
|
|
|
|
notFoundIn,
|
|
|
|
numeric,
|
|
|
|
integer,
|
|
|
|
file,
|
|
|
|
optional,
|
2021-06-02 01:59:40 +00:00
|
|
|
}
|