Nishant Nishant - 1 year ago 215
JSON Question

Ensure unique field value in loopback model

How to ensure uniqueness of a particular field in loopback model.
Like below is the model Post, I have a field genericId in it, I want it to be unique in the database, and loopback to through an error, on duplicate key insertion.

"name": "Post",
"plural": "Post",
"base": "PersistedModel",
"properties": {
"genericId": {
"type": "string",
"moderatedAt": {
"type": "date"
"validations": [],
"acls": [],
"methods": []

I have tried searching there documentation, and other examples but no success.
One solution which I can think of is, to create a remoteHook for the create function, and validate this field before inserting, but looking for some other way.

Answer Source

Set validation rule in your common/models/post.js

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download