Arya
The safest data layer for javascript. Complete with immutable properties, validation/error scheme Written with ES6 specificiations
Install
npm install --save arya
Prototype Methods
Usage
Extend the Arya constructor to create your new class
{ super; this; this; } Personprototypevalid_name = { return name && namelength > 3; } error: 'Name must be atleast 4 characters'; Personprototypevalid_age = { return ! && age >= 0 && age <= 1000; } error: 'Age must be a number between 0 and 1000'; var odin = 'odin' 1000;odin // trueodinname // 'odin'odin // 1000odinname'BAD' // 'BAD'odinname // falseodin // falseodin // { name: 'Name must be atleast 4 characters' };
Arya.prototype.noop(attribute)
object: a nooped object to that is used internally as a default setting for attributes
example
Aryaprototype;//returns { return true; } error: 'Invalid age'
Arya.prototype.errors()
object: an object of errors if any of the attributes on the current object have errors
example
var odin = 'odin' 1001;odin// returns age: 'Age must be a number between 0 and 1000'
Arya.prototype.isValid()
boolean: Checks if any properties have any errors
example
var odin = 'odin' 1001;odin// returnsfalse
Arya.prototype.toJSON()
object: Converts the respective attributes into a simple javascript object
example
var odin = 'odin' 1001;odin// returns name: 'odin' age: 1001
Arya.prototype.register(attribute, value)
Where most of the magic happens. Call this inside your constructor to overload attribute as a method with more methods!
example
var p = 'odin' 1001;pname// return name: Function valid: Function: valid error: Function: error validate: Function: validate
Instance Methods
These methods are loaded onto each property when the prototype register
method is called for that property.
instance[prop].valid([,boolean])
boolean: returns whether or not given property is valid. If argument is given, it will set the current validity to that argument. It gets reset the next time the validation function executes.
example
var p = 'foo' 100; pname;// return false pname;// return true pname;// return false pname;// return false
instance[prop].error()
string: Returns the error message for the given property, if the property is invalid. Default: 'Invalid ' + prop
.
example
var p = 'foo' 1000; p;// return 'Name must be atleast 4 characters
instance[prop].validate()
boolean: calls the validation function for the respective property and returns if valid
example
p = 'Odin' 100; pname;// return truepname;// return true pname// return false pname// return truepname// return true
Validation scheme
How do you add a validation function and error message to a property? The convention currently in place is to just add those
methods/error message to the prototype using the keyword valid_
+ attribute
.
The validation function gets called everytime the attribute changes.
The error message gets printing out when calling instance[prop].error()
if the attribute is invalid.
example
{ super; this; } Personprototypevalid_name = { return name && namelength > 2; } error: 'Name must be at least 3 characters';
Contributing
Issues, and pull requests welcome
- Fork it
- Create your feature branch (git checkout -b feature/my-new-feature)
- Commit your changes (git commit -am 'Add some feature')
- Push to the branch (git push origin feature/my-new-feature)
- Create a new Pull Request