Add new validators
This commit is contained in:
		
							parent
							
								
									60760fbb8d
								
							
						
					
					
						commit
						0bae015a01
					
				
					 1 changed files with 40 additions and 0 deletions
				
			
		|  | @ -1,6 +1,7 @@ | ||||||
| // License: LGPL-3.0-or-later
 | // License: LGPL-3.0-or-later
 | ||||||
| import * as Regex from './regex' | import * as Regex from './regex' | ||||||
| import {Field, Form} from "mobx-react-form"; | import {Field, Form} from "mobx-react-form"; | ||||||
|  | import moment = require("moment"); | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| interface ValidationInput { | 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 { |   static optional(validation:Validation) : Validation { | ||||||
|     return ({field, form, validator}:ValidationInput) => { |     return ({field, form, validator}:ValidationInput) => { | ||||||
|       if (!field.value || validator.isEmpty(field.value)){ |       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}` | ||||||
|  |       ] | ||||||
|  |     } | ||||||
|  |   } | ||||||
|  | 
 | ||||||
| 
 | 
 | ||||||
| } | } | ||||||
		Loading…
	
	Add table
		
		Reference in a new issue
	
	 Eric Schultz
						Eric Schultz