arya
v0.0.5
Published
A generic, lightweight, immutable data-layer for javascript
Downloads
6
Maintainers
Readme
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
class Person extends Arya {
constructor(name, age) {
super();
this.register('name', name);
this.register('age', age);
}
}
Person.prototype.valid_name = {
func: function(name) {
return name && name.length > 3;
},
error: 'Name must be atleast 4 characters'
};
Person.prototype.valid_age = {
func: function(age) {
return !isNaN(age) && age >= 0 && age <= 1000;
},
error: 'Age must be a number between 0 and 1000'
};
var odin = new Person('odin', 1000);
odin.isValid() // true
odin.name() // 'odin'
odin.age() // 1000
odin.name('BAD') // 'BAD'
odin.name.valid() // false
odin.isValid() // false
odin.errors() // { 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
Arya.prototype.noop('age');
//returns
{
func: function() { 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 = new Person('odin', 1001);
odin.errors()
// 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 = new Person('odin', 1001);
odin.isValid()
// returns
false
Arya.prototype.toJSON()
object: Converts the respective attributes into a simple javascript object
example
var odin = new Person('odin', 1001);
odin.toJSON()
// 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 = new Person('odin', 1001);
p.name
// 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 = new Person('foo', 100);
p.name.valid();
// return false
p.name.valid(true);
// return true
p.name.validate();
// return false
p.name.valid();
// 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 = new Person('foo', 1000);
p.error();
// 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 = new Person('Odin', 100);
p.name.valid();
// return true
p.name.validate();
// return true
p.name.valid(false)
// return false
p.name.validate()
// return true
p.name.valid()
// 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
class Person extends Arya {
constructor(name) {
super();
this.register('name', name);
}
}
Person.prototype.valid_name = {
func: function(name) {
return name && name.length > 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