kripod / bookshelf-validate Goto Github PK
View Code? Open in Web Editor NEWValidation for the Model objects of Bookshelf.js
License: MIT License
Validation for the Model objects of Bookshelf.js
License: MIT License
I have a problem when I update a set of records
so for example if I have a student and this student has a list of books.
if I call
let books = await Books.where({student_id: id}).save({active: false}, {
method: 'update',
patch: true
});
although I am only changing the active, bookshelf validator throughs name is required (where the name is a required field in books table)
Any idea?
How can one validate a field only if it's present?
Currently, If a value is null or undefined, validation always passes.
My use case is allowing null values for postcodes, but if one is provided then it must be valid.
So there either must be a valid one, or none at all.
I was hoping to achieve this with a custom validator:
postcode: [{
method: 'isPostcode',
error: 'Postcode is invalid',
args: { optional: true }
}]
Rough example:
module.exports = function (bookshelf, validator) {
'use strict';
let Model = bookshelf.Model.extend({
validationErrors: function () {
// existing code...
},
initialize: function () {
// attach event listener to do the validations on save
if (typeof this.validateOnSave === 'function') {
this.on('saving', this.validateSave);
}
},
validateOnSave: Promise.method(function () {
var errors = this.validationErrors();
if (errors == null) {
return null;
}
// Throw the errors or something so the model won't save.
})
});
bookshelf.Model = Model;
};
If you want to override initialize in a model then you need to do:
initialize: function () {
bookshelf.Model.prototype.initialize.apply(this);
// more initialization here
}
I'm currently using your plugin like this and it works really nicely, saves me a ton of boilerplate. Thanks!
I am using the isRequired validation for all fields in an Article model. I have a test that is failing even though all fields are present in this particular test. Here is my gist https://gist.github.com/Euklidian-Space/bf10fd1a72bec9190867854d1ea309d9
and here is my repo for the project https://github.com/Euklidian-Space/express-blog-api/tree/master/server
Thanks in advance
I received a strange error about this.collections
being undefined when I attempted to register the bookshelf-validate
plugin before registering my models. The error did not surface until calling into fetch
/ fetchAll
.
Do you know why? It took me awhile to figure out that the order of registration seems to matter. Thanks!
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.