Last active
January 15, 2019 22:56
-
-
Save cowboyd/d5ce2950ac9c3ab51864c797ef480ac5 to your computer and use it in GitHub Desktop.
A very, very rough draft of a custom field to perform validations on its input. (has not been tested)
This file contains 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
/** | |
* A higher-order microstate that takes a validation function, and returns | |
* a field class that validates the input of that field based on inputs. | |
*/ | |
export function Field(validationFn) { | |
return class Field extends String { | |
get validation() { | |
return validationFn(valueOf(this)); | |
} | |
get hasErrors() { | |
return this.validation.messages.length > 0; | |
} | |
} | |
} | |
This file contains 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 Field from './field'; | |
import { notEmpty } from './my-validation-lib'; | |
/** | |
* An example microstate composing two fields: | |
* | |
*/ | |
export class UserForm { | |
firstName = Field(notEmpty); | |
lastName = Field(notEmpty); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment