loopback icon indicating copy to clipboard operation
loopback copied to clipboard

strict: 'filter' does not ignore unknown properties

Open ernie58 opened this issue 9 years ago • 3 comments

I've made a repro case here: https://github.com/ernie58/loopback-sandbox/

When I put strict:'filter' on my models in the definition file, and I try to update a model via the loopback explorer (PATCH model/:id), unknown properties cause a validation error, instead they should be ignored. This is my definition file:

{
  "name": "Task",
  "base": "PersistedModel",
  "idInjection": true,
  "forceId": false,
  "strict": "filter",
  "indexes": {},
  "mixins": {},
  "scope": {},
  "validateUpsert": true,
  "properties": {
    "name": {
      "type": "string"
    },
    "description": {
      "type": "string"
    }
  },
  "validations": [],
  "relations": {},
  "acls": [
    {
      "accessType": "*",
      "principalType": "ROLE",
      "principalId": "$everyone",
      "permission": "ALLOW"
    }
  ],
  "methods": []
}

Description/Steps to reproduce

  • clone https://github.com/ernie58/loopback-sandbox and run yarn install
  • run node server/server.js
  • go to loopback explorer or run the curl command below
  • try to update the task model with JSON containing unknown properties:
curl -X PATCH --header "Content-Type: application/json" --header "Accept: application/json" -d "{\"foo\":\"bar\", \"name\":\"some-task\"}" "http://0.0.0.0:3000/api/Tasks/1"

Expected result

unknown properties should get ignored, the name of the task should get updated

actual result

{
  "error": {
    "statusCode": 422,
    "name": "ValidationError",
    "message": "The `Task` instance is not valid. Details: `foo` is not defined in the model (value: undefined).",
    "details": {
      "context": "Task",
      "codes": {
        "foo": [
          "unknown-property"
        ]
      },
      "messages": {
        "foo": [
          "is not defined in the model"
        ]
      }
    }
  }
}

Additional information

I did work in the default loopback version provided with the sandbox, it broke when I updated to latest loopback 3

linux x64 6.10.0

yarn list --prod --depth 0 | grep loopback ├─ [email protected] ├─ [email protected] ├─ [email protected] ├─ [email protected] ├─ [email protected] ├─ [email protected] ├─ [email protected] ├─ [email protected] ├─ [email protected] ├─ [email protected] ├─ [email protected]

ernie58 avatar Jul 11 '17 16:07 ernie58

I'm not sure but could this line be the culprit: https://github.com/strongloop/loopback-datasource-juggler/blob/b9129e69739ab703ae28f4ca84ea86849dd9a068/lib/dao.js#L3208

strict as a stringevaluates to true, which in turn triggers a strict check.

https://github.com/strongloop/loopback-datasource-juggler/blob/b9129e69739ab703ae28f4ca84ea86849dd9a068/lib/dao.js#L96 Here it adds props to the __unknownProperties anyway

When the isValid function is called, it then returns false because of here: https://github.com/strongloop/loopback-datasource-juggler/blob/3b45c76d0fd3364c55a55720d685b5596fb578d1/lib/validations.js#L507

ernie58 avatar Jul 13 '17 15:07 ernie58

Duplicate of https://github.com/strongloop/loopback-datasource-juggler/issues/1419.

ssh24 avatar Jul 17 '17 20:07 ssh24

Loopback-data-source juggler does not observe the setting "strict": true, for PATCH model/:id. Version 3.35.0 is still effected.

jamesjjk avatar May 11 '20 11:05 jamesjjk