Giter VIP home page Giter VIP logo

Comments (2)

sarus avatar sarus commented on July 22, 2024

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.

eventualbuddha avatar eventualbuddha commented on July 22, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.