Comments (2)
So I accomplished this by wrapping LGTM in my own object where I can chain together commonly used parameter validations such as limit, offset, sort etc. to create custom validators.
Looks something like this:
function Validator(){
this.lgtmValidator = LGTM.validator();
}
Validator.prototype.enabledOnly = function(){
this.lgtmValidator.validates('enabledOnly')
.optional()
.boolean('Must be a boolean');
return this;
};
Validator.prototype.includes = function(){
this.lgtmValidator.validates('includes')
.optional()
.string('Must be a string')
.includes(['id', 'username', 'full-name', 'email', 'is-admin', 'enabled', 'last-login'],
'The "includes" value must be "id", "username", "full_name", "email", or "is_admin"'
);
return this;
};
Validator.prototype.build = function(){
return this.lgtmValidator.build();
}
module.exports = Validator;
Then usage is:
var getUsersValidator = new Validator()
.enabledOnly()
.includes()
.build();
Thanks!
from lgtm.
Your approach seems like a reasonable one. I'm not opposed to adding something in core if it doesn't interfere with the existing flexibility to do what you ended up with. Perhaps changing the behavior of .build()
to return distinct objects rather than simply returning the same one over and over again?
from lgtm.
Related Issues (20)
- LGTM standalone | $$config$$config.async method is undefined HOT 2
- Question: why is validation response structure like it is? HOT 6
- add default messages HOT 3
- Ensure `required` calls without a message fail
- Publish sourcemaps HOT 2
- Dynamic messages HOT 1
- Online demo is broken HOT 1
- Add support for custom message HOT 5
- Version 10 of node.js has been released
- The automated release is failing 🚨 HOT 4
- Changing input values as part of validation? HOT 2
- Current version on NPM doesn't include commonjs build HOT 3
- An in-range update of mocha is breaking the build 🚨 HOT 8
- An in-range update of rollup is breaking the build 🚨 HOT 87
- An in-range update of gzip-size is breaking the build 🚨 HOT 2
- Dependent validations cause duplicate errors HOT 1
- An in-range update of uglify-js is breaking the build 🚨 HOT 17
- An in-range update of babel7 is breaking the build 🚨 HOT 18
- [Feature Request] Validations on a nested object. HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from lgtm.