Search code examples
javascriptloopbackjsstrongloop

How to use the `if` options on `validatesUniquenessOf` of Loopback?


The loopback documentation describes a validation method validatesUniquenessOf which has an option to add if property. But there are no examples provided. How do I write something like this?

I have the following context:

The model JSON file:

{ "name": "CategoryKit", "properties": { "isDeleted": { "type": "boolean", "default": false } }, "relations": { "category": { "type": "belongsTo", "model": "Category", "foreignKey": "categoryId" }, "kit": { "type": "belongsTo", "model": "Kit", "foreignKey": "kitId" } }, }

The model javascript file:

module.exports = function(CategoryKit) { CategoryKit.validatesUniquenessOf("kitId", { scopedTo: ["categoryId"] }); }

Now, i want that this validation working if only the isDeleted property is equal false.


Solution

  • This is the example on the loopback api docs

     SiteUser.validateUniquenessOf('login', { scopedTo: ['siteId'] });
    

    For your particular use case, can you see if this works?

    SiteUser.validateUniquenessOf('mobile', { if: '90443123' });