lib/src/forms/rules/arrays.ts

151 lines
3.8 KiB
TypeScript
Raw Normal View History

2021-06-03 03:36:25 +00:00
import {ValidationResult, ValidatorFunction} from './types'
2021-06-02 01:59:40 +00:00
2021-06-03 03:36:25 +00:00
/** Requires the input value to be an array. */
function is(fieldName: string, inputValue: unknown): ValidationResult {
if ( Array.isArray(inputValue) ) {
return { valid: true }
}
return {
valid: false,
message: 'must be an array',
}
}
/** Requires the values in the input value array to be distinct. */
function distinct(fieldName: string, inputValue: unknown): ValidationResult {
const arr = is(fieldName, inputValue)
if ( !arr.valid ) {
return arr
}
if ( Array.isArray(inputValue) && (new Set(inputValue)).size === inputValue.length ) {
return { valid: true }
}
return {
valid: false,
message: 'must not contain duplicate values',
}
}
/**
* Builds a validator function that requires the input array to contain the given value.
* @param value
*/
function includes(value: unknown): ValidatorFunction {
return (fieldName: string, inputValue: unknown): ValidationResult => {
const arr = is(fieldName, inputValue)
if ( !arr.valid ) {
return arr
}
if ( Array.isArray(inputValue) && inputValue.includes(value) ) {
2021-06-02 01:59:40 +00:00
return { valid: true }
}
return {
valid: false,
2021-06-03 03:36:25 +00:00
message: `must include ${value}`,
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 input array NOT to contain the given value.
* @param value
*/
function excludes(value: unknown): ValidatorFunction {
return (fieldName: string, inputValue: unknown): ValidationResult => {
2021-06-02 01:59:40 +00:00
const arr = is(fieldName, inputValue)
2021-06-03 03:36:25 +00:00
if ( !arr.valid ) {
return arr
}
2021-06-02 01:59:40 +00:00
2021-06-03 03:36:25 +00:00
if ( Array.isArray(inputValue) && !inputValue.includes(value) ) {
2021-06-02 01:59:40 +00:00
return { valid: true }
}
return {
valid: false,
2021-06-03 03:36:25 +00:00
message: `must not include ${value}`,
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 input array to have exactly `len` many entries.
* @param len
*/
function length(len: number): ValidatorFunction {
return (fieldName: string, inputValue: unknown): ValidationResult => {
const arr = is(fieldName, inputValue)
if ( !arr.valid ) {
return arr
2021-06-02 01:59:40 +00:00
}
2021-06-03 03:36:25 +00:00
if ( Array.isArray(inputValue) && inputValue.length === len ) {
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 exactly of length ${len}`,
2021-06-02 01:59:40 +00:00
}
}
2021-06-03 03:36:25 +00:00
}
/**
* Builds a validator function that requires the input array to have at least `len` many entries.
* @param len
*/
function lengthMin(len: number): ValidatorFunction {
return (fieldName: string, inputValue: unknown): ValidationResult => {
const arr = is(fieldName, inputValue)
if ( !arr.valid ) {
return arr
}
2021-06-02 01:59:40 +00:00
2021-06-03 03:36:25 +00:00
if ( Array.isArray(inputValue) && inputValue.length >= len ) {
return { valid: true }
}
return {
valid: false,
message: `must be at least length ${len}`,
2021-06-02 01:59:40 +00:00
}
}
2021-06-03 03:36:25 +00:00
}
/**
* Builds a validator function that requires the input array to have at most `len` many entries.
* @param len
*/
function lengthMax(len: number): ValidatorFunction {
return (fieldName: string, inputValue: unknown): ValidationResult => {
const arr = is(fieldName, inputValue)
if ( !arr.valid ) {
return arr
}
2021-06-02 01:59:40 +00:00
2021-06-03 03:36:25 +00:00
if ( Array.isArray(inputValue) && inputValue.length <= len ) {
return { valid: true }
}
return {
valid: false,
message: `must be at most length ${len}`,
2021-06-02 01:59:40 +00:00
}
}
}
2021-06-03 03:36:25 +00:00
export const Arr = {
is,
distinct,
includes,
excludes,
length,
lengthMin,
lengthMax,
}