Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dot-build/gisele-validation
Validation extension to Gisele library
https://github.com/dot-build/gisele-validation
Last synced: about 2 months ago
JSON representation
Validation extension to Gisele library
- Host: GitHub
- URL: https://github.com/dot-build/gisele-validation
- Owner: dot-build
- Created: 2015-10-12T09:34:39.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2015-12-14T01:21:44.000Z (about 9 years ago)
- Last Synced: 2024-04-14T08:30:17.441Z (10 months ago)
- Language: JavaScript
- Size: 23.4 KB
- Stars: 1
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Validation extension to Gisele model library
Adds validation capabilities to any Gisele.Model instances
## Usage
```js
var User = Gisele.Model.create({
name: { type: String, required: true, minlength: 15 },
age: { type: Number, min: 18 }
});var bob = new User({
name: 'Bob',
age: 15
});// runs the validation rules of each field
bob.$$.validate();console.log(bob.$$invalid);
// trueconsole.log(bob.$$errors);
// {
// name: { minlength: true },
// age: { min: true }
// }```
## Validation of custom fields
New field types must implement a `validate()` method that either returns `true` (is valid) or an object with the validation errors.
In the builtin fields they are key/value pairs with the key being an error and the value being true, as same as `bob.$$errors` above.
Example:
```js
class Foo() { }
class FooField extends Gisele.Field {
parse(value) { return new Foo(value); }validate(value) {
// any validation rule can be applied
if (this.required && value instanceof Foo) {
return { required: true };
}return true;
}
}Gisele.Field.add(Foo, FooField);
```