Giter Site home page Giter Site logo

Required if validator about vue-rawmodel HOT 6 CLOSED

xpepermint avatar xpepermint commented on July 20, 2024
Required if validator

from vue-rawmodel.

Comments (6)

xpepermint avatar xpepermint commented on July 20, 2024

That should be easy to achieve just by adding a custom validator (on your model schema). Note that contextable.js is using objectschema.js and validatable.js under the hood thus you will find details there.

Here is just a hint on how your schema should look like (check the docs for details):

export const schema = new Schema({
  fields: {
    name: {
      type: 'String',
      validate: [
        {
          validator: 'myCustomValidator',
          message: 'is required'
        }
      ]
    }
  },
  validatorOptions: { // this is passed directly to validatable.js Validator class
     validators: {
        async myCustomValidator ({value, recipe}}) { // function or promise
           return this.field1 && this.field2 && this.somethingElse; // return true when valid
        }
     }
  }
});

Note also that a validator's context is Model so you can access model's fields by using this.{field}. If you have nested structure, then you access parent model as this.$parent. Let me know if that helps.

from vue-rawmodel.

xpepermint avatar xpepermint commented on July 20, 2024

Maybe we could add the if or only key which would run field validation only if listed validators are true. If you think this is a good idea, you can open a new issue here.

from vue-rawmodel.

xpepermint avatar xpepermint commented on July 20, 2024

Feature requested here rawmodel/framework#2.

from vue-rawmodel.

michaelsorich avatar michaelsorich commented on July 20, 2024

Thanks - the example of the custom validator was sufficient for my needs although it would be simpler if there was an if key for the presence validator (not so sure about the use cases more generally).

from vue-rawmodel.

xpepermint avatar xpepermint commented on July 20, 2024

@michaelsorich cool. The feature you requested will be added in one of the future releases.

from vue-rawmodel.

xpepermint avatar xpepermint commented on July 20, 2024

The latest contextable.js now provides the condition option.

from vue-rawmodel.

Related Issues (13)

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.