Skip to content

Instantly share code, notes, and snippets.

@cowboyd
Last active January 15, 2019 22:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cowboyd/d5ce2950ac9c3ab51864c797ef480ac5 to your computer and use it in GitHub Desktop.
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)
/**
* 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;
}
}
}
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