Search code examples
loopbackjsloopback

in loopback model.json file how to give validations


In model.json file I try to give validation property to array as per document says but it doesn't work .. (I want validaions by validations prperty only) How to give validations array ?..

also see i tried as per in screenshort but that didn't workenter image description here


Solution

  • I am new to Loopback but the current (v3) documentation states:

    Warning: This is not yet implemented. You must currently validate in code; see Validating model data.

    For now you have to put your validation code in the model's JS file.

    This is the example given in the documentation:

    common/models/user.js

    module.exports = function(user) {
      user.validatesPresenceOf('name', 'email');
      user.validatesLengthOf('password', {min: 5, message: {min: 'Password is too short'}});
      user.validatesInclusionOf('gender', {in: ['male', 'female']});
      user.validatesExclusionOf('domain', {in: ['www', 'billing', 'admin']});
      user.validatesNumericalityOf('age', {int: true});
      user.validatesUniquenessOf('email', {message: 'email is not unique'});
    };
    

    This is reminiscent of Angular's Reactive form validation.