Skip to content

Instantly share code, notes, and snippets.

@ReVoid
Created October 4, 2023 09:22
Show Gist options
  • Save ReVoid/8b426bff092f95d23f2466ed5992f8ab to your computer and use it in GitHub Desktop.
Save ReVoid/8b426bff092f95d23f2466ed5992f8ab to your computer and use it in GitHub Desktop.
type Validator<T> = (value: T) => boolean;
class FormInput<T> {
constructor(public value: T, public validators: Validator<T>[]){}
get isValid(): boolean {
return this.validators.every(v => v(this.value));
}
}
class FormGroup<T extends object> {
constructor(public inputs: { [K in keyof T]: FormInput<T[K]>}) {}
get isValid(): boolean {
return Object.values(this.inputs).every((v: any) => v.isValid === true)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment