Realy simple and customizable schemas without recursion.
- Multivariant typing
- Super fast one while implementation
- Custom operators
npm install --save surfing
// Example of custom DBRef/simple number pointer to some document
var ref = {
or: {
object: {
props: {
id: {
string: true
},
collection: {
string: true
}
}
},
number: true
}
};
var surfing = new Surfing({
object: {
props: {
source: ref,
target: ref
}
}
}, {
source: { id: '507f1f77bcf86cd799439011', collection: 'users' },
target: 12345
});
surfing.travers();
surfing.errors.length; // 0
var Surfing = require('surfing'); var surfing = new Surfing(schema?: Schema, data?: any, options?: Options);
Constructor for a one surfing instance.
surfing.travers();
Run travers by the scheme and data.
surfing.errors: [{ error?: any, schemaPath: [any], dataPath: [any], operatorsPath: [any] }]
Stores error if they throwed as a result of the traversing.
var Finding = require('surfing/finding'); var finding = new Surfing(schema?: Schema, data?: any, options?: Options);
Targeted traversing of the tree in search of appropriate path.
finding.byOperators(...path: [any]) => finding
Find stack level by operators path.
Set finding.fail = true
if the search failed.
finding.bySchema(...path: [any]) => finding
Find stack level by schema path.
Set finding.fail = true
if the search failed.
finding.byData(...path: [any]) => finding
Find stack level by data path.
Set finding.fail = true
if the search failed.
Object
Global settings for surfing instance.
Object = Surfing.dictionary
In this option you can set a custom dictionary operators.
Boolean = true
Defines the detail of error when traversing.
true
by default, all errors will be saved, all paths will be traversedfalse
,and
operator will stop after the first error,or
operator throws one's own error
Boolean = true
It allows you to disable validation on traversing. By default is enabled.
Function = function(surfing) {}
Handler before execute the logic of the stack level.
Function = function(surfing) {}
Handler after execute the logic of the stack level.
Function = function(surfing) {}
Handler before call operator of the stack level.
Function = function(surfing) {}
Handler after call operator of the stack level.
{
string: true,
custom: function(surfing) {/* do something */}
}
[
'string',
function(surfing) {/* do something */}
]
{
string: true,
number: true
}
[
'string',
'number'
]
Allows accurately move stack to a specific key in the data
var data = {
abc: 123
};
var schema = {
object: {
props: {
abc: {
number: {
equal: 123
}
}
}
}
}
It allows you to apply a description to all content.
var data = {
abc: 123,
cde: 123
};
var schema = {
object: {
each: {
number: {
equal: 123
}
}
}
}
All operators of typing take and
operator as scheme.
{
string: {
min: 10,
max: 30
},
number: {
greater: 15
}
}
Applicable for strings, numbers or arrays.
{
or: {
string: {
equal: 'abc'
},
number: {
greater: 3,
less: 31
},
array: {
min: 3,
max: 30
}
}
}
It allows to set value under certain conditions.
var data = {
abc: 123
};
var schema = {
object: {
props: {
abc: {
number: {
set: 234
}
}
}
}
}
var surfing = new Surfing(schema, data);
surfing.travers();
surfing.data.abc; // 234
It allows to delete value under certain conditions.
var data = {
abc: 123
};
var schema = {
object: {
props: {
abc: {
number: {
delete: true
}
}
}
}
}
var surfing = new Surfing(schema, data);
surfing.travers();
surfing.data; // {}
It sets the value if it did not exist before.
var data = {};
var schema = {
object: {
props: {
abc: {
default: 123,
number: {
max: 234
}
}
}
}
}
var surfing = new Surfing(schema, data);
surfing.travers();
surfing.data; // { abc: 123 }
Performs a custom action.
{
custom: function(surfing) {
if (surfing.getData() == 123)
surfing.throw('My custom error');
}
}
[
function(surfing) {
if (surfing.getData() == 123)
surfing.throw('My custom error');
}
]
Verify with regular expression.
{
regex: /^\d+$/
}
- Date operator
- Search in scheme by path
- Merge Validation into Surfing class
- Documentation
- Options for
Surfing
andValidating
classes - Move dictionary and handler to options
- Default dictionary
- Option
details?: boolean = true
- Support for array and/or difinition
- Comparison and regex operators
- pathSchema and pathData added to stack
- Operators: equal, set, delete, default
- Fix many operator's bugs
- Operators: null, nan, custom and each
- Remove temp comments
- Fix "or" bug
- Operators tests
- Performance tests
- Initial commit
- Simple surfing class
- Validation surfing class