mirror of
				https://codeberg.org/yeentown/barkey.git
				synced 2025-10-26 19:14:12 +00:00 
			
		
		
		
	wip
This commit is contained in:
		
							parent
							
								
									168039e682
								
							
						
					
					
						commit
						7f4db37ff4
					
				
					 1 changed files with 99 additions and 0 deletions
				
			
		
							
								
								
									
										99
									
								
								src/api/validator2.ts
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										99
									
								
								src/api/validator2.ts
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,99 @@ | |||
| import * as mongo from 'mongodb'; | ||||
| import hasDuplicates from '../common/has-duplicates'; | ||||
| 
 | ||||
| type CustomValidator<T> = (value: T) => boolean | string; | ||||
| 
 | ||||
| interface Validator { | ||||
| 	get: () => [any, string]; | ||||
| 
 | ||||
| 	required: () => Validator; | ||||
| 
 | ||||
| 	validate: (validator: CustomValidator<any>) => Validator; | ||||
| } | ||||
| 
 | ||||
| class ValidatorCore implements Validator { | ||||
| 	value: any; | ||||
| 	error: string; | ||||
| 
 | ||||
| 	required() { | ||||
| 		if (this.error === null && this.value === null) { | ||||
| 			this.error = 'required'; | ||||
| 		} | ||||
| 		return this; | ||||
| 	} | ||||
| 
 | ||||
| 	get(): [any, string] { | ||||
| 		return [this.value, this.error]; | ||||
| 	} | ||||
| 
 | ||||
| 	validate(validator: any) { | ||||
| 		if (this.error || this.value === null) return this; | ||||
| 		const result = validator(this.value); | ||||
| 		if (result === false) { | ||||
| 			this.error = 'invalid-format'; | ||||
| 		} else if (typeof result == 'string') { | ||||
| 			this.error = result; | ||||
| 		} | ||||
| 		return this; | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| class NumberValidator extends ValidatorCore { | ||||
| 	value: number; | ||||
| 	error: string; | ||||
| 
 | ||||
| 	constructor(value) { | ||||
| 		super(); | ||||
| 		if (value === undefined || value === null) { | ||||
| 			this.value = null; | ||||
| 		} else if (!Number.isFinite(value)) { | ||||
| 			this.error = 'must-be-a-number'; | ||||
| 		} else { | ||||
| 			this.value = value; | ||||
| 		} | ||||
| 	} | ||||
| 
 | ||||
| 	range(min: number, max: number) { | ||||
| 		if (this.error || this.value === null) return this; | ||||
| 		if (this.value < min || this.value > max) { | ||||
| 			this.error = 'invalid-range'; | ||||
| 		} | ||||
| 		return this; | ||||
| 	} | ||||
| 
 | ||||
| 	required() { | ||||
| 		return super.required(); | ||||
| 	} | ||||
| 
 | ||||
| 	get(): [number, string] { | ||||
| 		return super.get(); | ||||
| 	} | ||||
| 
 | ||||
| 	validate(validator: CustomValidator<number>) { | ||||
| 		return super.validate(validator); | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| const it = (value) => { | ||||
| 	return { | ||||
| 		must: { | ||||
| 			be: { | ||||
| 				a: { | ||||
| 					string: 0, | ||||
| 					number: () => new NumberValidator(value), | ||||
| 					boolean: 0, | ||||
| 					set: 0 | ||||
| 				}, | ||||
| 				an: { | ||||
| 					id: 0, | ||||
| 					array: 0, | ||||
| 					object: 0 | ||||
| 				} | ||||
| 			} | ||||
| 		} | ||||
| 	}; | ||||
| }; | ||||
| 
 | ||||
| export default it; | ||||
| 
 | ||||
| const [n, e] = it(42).must.be.a.number().required().range(10, 70).validate(x => x != 21).get(); | ||||
		Loading…
	
	Add table
		
		Reference in a new issue