Last active
May 6, 2017 16:33
-
-
Save notgiorgi/3dca890c5877fafba39d4dbeeebda5d1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Validation, { Success, Failure } from 'data.validation' | |
// დატა ფილდები ინტელისენსისთვის | |
interface Validator<T, V> { | |
criteria: T, | |
fieldName: String, | |
value: V, | |
result(): Validation, | |
} | |
class MinLength implements Validator<Number, String>{ | |
constructor ( | |
private criteria: Number, | |
private fieldName: String, | |
private value: String, | |
) { } | |
result () { | |
return this.value.length >= this.criteria | |
? Success(this.value) | |
: Failure([`field: ${this.fieldName}, minimum length should be ${this.criteria}`]) | |
} | |
} | |
class MinValue implements Validator<Number, Number> { | |
constructor ( | |
private criteria: Number, | |
private fieldName: String, | |
private value: Number, | |
) { } | |
result () { | |
return this.value >= this.criteria | |
? Success(this.value) | |
: Failure([`field: ${this.fieldName}, minimum value should be ${this.criteria}`]) | |
} | |
} | |
// მასივი გვაქვს იმ თანმიმდევრობით, რომლითაც ვაწვდით კონსტრუქტორის არგუმენტებს | |
// ყოველ ვალიდატორში ყველა წინას შედეგიც გვაქვს თუ დაგვჭირდა | |
// behind the scenes `Validator` იყენებს Success/Failures-ს მონადურ სტრუქტურას | |
// იცის როგორ "შეაჯამოს" შეცდომები, პირველივე შეცდომაზე short circuit აკეთებს, მარა ყოველ შემდეგ შეცდომასაც ინახავს | |
@Validation([ | |
name => new MinLength(3, 'name', name), | |
(name, age) => new MinValue(13, 'age', age), | |
(name, age, address) => new MinLength(10, 'address', address), | |
]) | |
class Person { | |
constructor( | |
private name: String, | |
private age: Number, | |
private Address: String | |
) { } | |
/** | |
* Any method you like. | |
*/ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment