Security News
ESLint is Now Language-Agnostic: Linting JSON, Markdown, and Beyond
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
racer-schema
Advanced tools
Current 0.7 version of ShareJS has 4 hooks which are executed while operation is applied.
s ok for some ops (model.set, model.add) because they ovewrites previous data (or add new), but for array mutators (model.push, model.pop, etc), string mutators (model.stringInsert, model.stringRemove) and increment (model.increment) it
s impossible to predict what will be the result data and we can not use some kinds of validators here because of this, like max, min, etc. That`s why we should use sync hook.All schema validation logic executes in 'validate' hook. Custom validators have two methods: '.sync' - sync and executes in 'validate' hook and '.async' - async and executes in 'submit' hook. There can be one of them or both, also you can preload data in '.async' method to use it later in '.sync'
npm install racer-schema
var options = {
// Schemas use JSON-Schema format
schemas: {
users: {
title: 'Example Schema',
type: 'object',
properties: {
nickname: {
type: 'string',
format: 'xstring', // custom format
minLength: 1,
maxLength: 10
},
email: {
type: 'string',
format: 'email'
},
age: {
description: 'Age in years',
type: 'integer',
minimum: 0
},
roleId: {
type: 'string',
collection: 'roles', // additional field for 'join' custom validator
validators: ['join'] // custom validators
},
hobbies: {
type: 'array',
maxItems: 3,
items: {
type: 'string'
},
uniqueItems: true
}
},
required: ['email']
},
roles: {
type: 'object',
properties: {
name: {
type: 'string'
}
}
}
},
// JSON-Schema formats can be added here. They should be sync
formats: {
xstring: function(str) {
return str !== 'xxx';
}
},
// Custom validators
validators: {
// join - is working example of custom validator. It ensures that value is id of doc of specific collection
join: {
async: function(context, done) {
var id = context.value; // here is value for this op
if (!id) return done();
var collection = context.schema.collection; // context.schema - is schema of current property
var model = this.store.createModel();
var $entity = model.at(collection + '.' + id);
model.fetch($entity, function(err) {
if (err) return done(err);
if (!$entity.get()) {
return done(Error('No ' + collection + ' with id ' + id));
}
done();
});
}
},
// this is example of custom validator, that preloads data and uses it later
preload: {
async: function(context, done) {
var model = this.store.createModel(); // that`s how to get model
var $someData = model.at('some.path');
model.fetch($someData, function(err) {
if (err) return done(err);
var data = $someData.get();
done(null, data); // pass data as second parameter
})
},
sync: function(value, context) {
var data = context.data; // preloaded data is here
retturn true || false;
}
}
}
}
racer.use(require('racer-schema'), options);
// Or
derby.use(require('racer-schema'), options);
model.add('collection', doc, function(err) {
if (err) {
var errors = err.errors;
for (var i = 0; i < errors.length; i++) {
var error = errors[i];
var message = error.message; // Error message
var paths = error.paths; // Array of strings. Full path to error field
}
}
});
Copyright (c) 2014 Vladimir Makhaev
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
FAQs
Schema plugin for Racer
The npm package racer-schema receives a total of 32 weekly downloads. As such, racer-schema popularity was classified as not popular.
We found that racer-schema demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
Security News
Members Hub is conducting large-scale campaigns to artificially boost Discord server metrics, undermining community trust and platform integrity.
Security News
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.