diff --git a/javascripts/src/lib/vjf_rules.ts b/javascripts/src/lib/vjf_rules.ts index 795e5468..ad40e006 100644 --- a/javascripts/src/lib/vjf_rules.ts +++ b/javascripts/src/lib/vjf_rules.ts @@ -1,6 +1,7 @@ // License: LGPL-3.0-or-later import * as Regex from './regex' import {Field, Form} from "mobx-react-form"; +import moment = require("moment"); interface ValidationInput { @@ -42,6 +43,35 @@ export class Validations { ] } + static isNumber({field, validator}:ValidationInput):StringBoolTuple { + return [ + !isNaN(parseFloat(field.value)), + `${field.label} must be a number` + ] + } + + + static isGreaterThanOrEqualTo(value:number) : ({field, validator}:ValidationInput) => StringBoolTuple + { + return ({field, validator}:ValidationInput) => { + return [ + parseFloat(field.value) >= value, + `${field.label} must be at least ${value}` + ] + } + } + + static isLessThanOrEqualTo(value:number, flip:boolean=false) : ({field, validator}:ValidationInput) => StringBoolTuple + { + return ({field, validator}:ValidationInput) => { + let float = parseFloat(field.value) + return [ + (flip ? -1 * float : float) <= value, + `${field.label} must be no more than ${value}` + ] + } + } + static optional(validation:Validation) : Validation { return ({field, form, validator}:ValidationInput) => { if (!field.value || validator.isEmpty(field.value)){ @@ -53,5 +83,15 @@ export class Validations { }; } + static isDate(format:string): ({field, validator}:ValidationInput) => StringBoolTuple { + return ({field, validator}:ValidationInput) => { + let m = moment(field.value, format, true); + return [ + m.isValid(), + `${field.label} must be a date with format: ${format}` + ] + } + } + } \ No newline at end of file