Skip to content
This repository has been archived by the owner on Aug 29, 2018. It is now read-only.

Support array objects as data #64

Closed
ghost opened this issue Mar 7, 2017 · 6 comments
Closed

Support array objects as data #64

ghost opened this issue Mar 7, 2017 · 6 comments

Comments

@ghost
Copy link

ghost commented Mar 7, 2017

Steps to reproduce

const data = [
    {
        hello: "world"
    }
];
throw new errors.BadRequest("Input not valid", data);

Expected behavior

Response:

{
    "name": "BadRequest",
     "message": "JSON-Schema validation failed",
     "code": 400,
     "className": "bad-request",
     "data": [
        {
            "hello": "world"
        }
     ],
    "errors":  { }
}

Actual behavior

Response:

{
    "name": "BadRequest",
     "message": "JSON-Schema validation failed",
     "code": 400,
     "className": "bad-request",
     "data": {
        "0": {
            "hello": "world"
        }
     },
    "errors":  { }
}

More

The problem is here:
https://github.com/feathersjs/feathers-errors/blob/master/src/index.js#L36

Proposed solution

If we could make the 'errors' property of the data object a parameter instead, we won't have the problem of messing with immutable data and it will also enable us to pass an error object, alongside with an array object as data.

@daffl
Copy link
Member

daffl commented Mar 8, 2017

I have a fix for this in #65. Your proposed solution does make sense. The main reason to have an errors property on data was because a lot of the ORM errors already do that. We could add it as another additional parameter but you'd always have to pass an empty data object before the errors.

@ghost
Copy link
Author

ghost commented Mar 8, 2017

I think the additional parameter should work, it's better than not being able to pass the errors object at all and it will also keep backwards compatibility.

@daffl
Copy link
Member

daffl commented Mar 9, 2017

Can you show an example how you would like to call it?

@ghost
Copy link
Author

ghost commented Mar 9, 2017

Example:

const errors = [
    {
      "keyword": "required",
      "dataPath": "",
      "schemaPath": "#/required",
      "params": {
        "missingProperty": "customerId"
      },
      "message": "should have required property 'customerId'"
    }
];
throw new errors.BadRequest("Input validation failed", null, errors);

And in the code, if errors is undefined, we can check the data object for the errors property. No breaking changes.

@kernelwhisperer
Copy link
Contributor

Should I work on a pull request?
Is this the desired solution?

@ekryski
Copy link
Member

ekryski commented Mar 31, 2017

Sorry late to the game here but any reason data has to be an array or was the issue just that you want errors to be an array? The module should support both already. The only thing you need to do is call it like so:

const errors = [
    {
      "keyword": "required",
      "dataPath": "",
      "schemaPath": "#/required",
      "params": {
        "missingProperty": "customerId"
      },
      "message": "should have required property 'customerId'"
    }
];

throw new errors.BadRequest('Input validation failed', { errors });

Which should result in this:

{
  "name": "BadRequest",
  "message": "JSON-Schema validation failed",
  "code": 400,
  "className": "bad-request",
  "data": {},
  "errors":  [
    {
      "keyword": "required",
      "dataPath": "",
      "schemaPath": "#/required",
      "params": {
        "missingProperty": "customerId"
      },
      "message": "should have required property 'customerId'"
    }
  ]
}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants