mirror of
				https://codeberg.org/yeentown/barkey.git
				synced 2025-10-26 19:14:12 +00:00 
			
		
		
		
	Custom validator support
This commit is contained in:
		
							parent
							
								
									73deef0ca7
								
							
						
					
					
						commit
						6256a8bdfc
					
				
					 1 changed files with 7 additions and 1 deletions
				
			
		|  | @ -2,7 +2,7 @@ import * as mongo from 'mongodb'; | ||||||
| 
 | 
 | ||||||
| type Type = 'id' | 'string' | 'number' | 'boolean' | 'array' | 'object'; | type Type = 'id' | 'string' | 'number' | 'boolean' | 'array' | 'object'; | ||||||
| 
 | 
 | ||||||
| export default <T>(value: any, isRequired: boolean, type: Type): [T, string] => { | export default <T>(value: any, isRequired: boolean, type: Type, validator?: (any) => boolean): [T, string] => { | ||||||
| 	if (value === undefined || value === null) { | 	if (value === undefined || value === null) { | ||||||
| 		if (isRequired) { | 		if (isRequired) { | ||||||
| 			return [null, 'is-required'] | 			return [null, 'is-required'] | ||||||
|  | @ -49,5 +49,11 @@ export default <T>(value: any, isRequired: boolean, type: Type): [T, string] => | ||||||
| 			break; | 			break; | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  | 	if (validator) { | ||||||
|  | 		if (!validator(value)) { | ||||||
|  | 			return [null, 'invalid-format']; | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
| 	return [value, null]; | 	return [value, null]; | ||||||
| }; | }; | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		
		Reference in a new issue