lib/src/forms/rules/numeric.ts

211 lines
5.3 KiB
TypeScript
Raw Normal View History

2021-06-03 03:36:25 +00:00
import {ValidationResult, ValidatorFunction} from './types'
/**
* Builds a validator function that requires the input value to be greater than some value.
* @param value
*/
function greaterThan(value: number): ValidatorFunction {
return (fieldName: string, inputValue: unknown): ValidationResult => {
if ( Number(inputValue) > value ) {
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 greater than ${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 value to be at least some value.
* @param value
*/
function atLeast(value: number): ValidatorFunction {
return (fieldName: string, inputValue: unknown): ValidationResult => {
if ( Number(inputValue) >= value ) {
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 at least ${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 value to be less than some value.
* @param value
*/
function lessThan(value: number): ValidatorFunction {
return (fieldName: string, inputValue: unknown): ValidationResult => {
if ( Number(inputValue) < value ) {
return { valid: true }
}
return {
valid: false,
message: `must be less than ${value}`,
2021-06-02 01:59:40 +00:00
}
}
2021-06-03 03:36:25 +00:00
}
/**
* Builds a validator function that requires the input value to be at most some value.
* @param value
*/
function atMost(value: number): ValidatorFunction {
return (fieldName: string, inputValue: unknown): ValidationResult => {
if ( Number(inputValue) <= value ) {
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 at most ${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 value to have exactly `num` many digits.
* @param num
*/
function digits(num: number): ValidatorFunction {
return (fieldName: string, inputValue: unknown): ValidationResult => {
if ( String(inputValue).replace('.', '').length === num ) {
return { valid: true }
}
return {
valid: false,
message: `must have exactly ${num} digits`,
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 value to have at least `num` many digits.
* @param num
*/
function digitsMin(num: number): ValidatorFunction {
return (fieldName: string, inputValue: unknown): ValidationResult => {
if ( String(inputValue).replace('.', '').length >= num ) {
return { valid: true }
}
return {
valid: false,
message: `must have at least ${num} digits`,
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 value to have at most `num` many digits.
* @param num
*/
function digitsMax(num: number): ValidatorFunction {
return (fieldName: string, inputValue: unknown): ValidationResult => {
if ( String(inputValue).replace('.', '').length <= num ) {
return { valid: true }
}
return {
valid: false,
message: `must have at most ${num} digits`,
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 value to end with the given number sequence.
* @param num
*/
function ends(num: number): ValidatorFunction {
return (fieldName: string, inputValue: unknown): ValidationResult => {
if ( String(inputValue).endsWith(String(num)) ) {
return { valid: true }
}
return {
valid: false,
message: `must end with "${num}"`,
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 value to begin with the given number sequence.
* @param num
*/
function begins(num: number): ValidatorFunction {
return (fieldName: string, inputValue: unknown): ValidationResult => {
if ( String(inputValue).startsWith(String(num)) ) {
2021-06-02 01:59:40 +00:00
return { valid: true }
}
return {
valid: false,
2021-06-03 03:36:25 +00:00
message: `must begin with "${num}"`,
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 value to be a multiple of the given number.
* @param num
*/
function multipleOf(num: number): ValidatorFunction {
return (fieldName: string, inputValue: unknown): ValidationResult => {
if ( parseFloat(String(inputValue)) % num === 0 ) {
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 a multiple of ${num}`,
2021-06-02 01:59:40 +00:00
}
}
}
2021-06-03 03:36:25 +00:00
/** Requires the input value to be even. */
function even(fieldName: string, inputValue: unknown): ValidationResult {
if ( parseFloat(String(inputValue)) % 2 === 0 ) {
return { valid: true }
}
return {
valid: false,
message: 'must be even',
}
}
/** Requires the input value to be odd. */
function odd(fieldName: string, inputValue: unknown): ValidationResult {
if ( parseFloat(String(inputValue)) % 2 === 0 ) {
return { valid: true }
}
return {
valid: false,
message: 'must be odd',
}
}
export const Num = {
greaterThan,
atLeast,
lessThan,
atMost,
digits,
digitsMin,
digitsMax,
ends,
begins,
multipleOf,
even,
odd,
}