Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nested errors with JSON Schema #593

Closed
adrianschneider94 opened this issue Sep 10, 2019 · 4 comments
Closed

Nested errors with JSON Schema #593

adrianschneider94 opened this issue Sep 10, 2019 · 4 comments
Assignees
Labels
Type: Bug Bug reports and their fixes

Comments

@adrianschneider94
Copy link

adrianschneider94 commented Sep 10, 2019

Hi,

inline errors for nested schemas don't work with JSON Schema. At least not like shown in the docs.
getError(name, error) compares the paths, but the path ajv returns has / as delimiter whilst uniforms uses ..

You can make it work by transforming the ajv errors:

let adaptPath = validator.errors.map((error: any) => ({...error, dataPath: error.dataPath.replace(/\//g, ".")}))

However, I think it would be better to change the JSONSchemaBridge accordingly.

Kind regards and thanks for your work!

@radekmie radekmie self-assigned this Sep 12, 2019
@radekmie radekmie added the Type: Question Questions and other discussions label Sep 12, 2019
@radekmie
Copy link
Contributor

Hi @adrianschneider94. Yes, ajv actually uses / but only for schemaPath. We do use dataPath in JSONSchemaBridge, which is dot-separated.

Playground example. If you type x in the input, and submit it, the error looks like this:

{
  "details": [
    {
      "keyword": "pattern",
      "dataPath": ".a.b.c",
      "schemaPath": "#/properties/a/properties/b/properties/c/pattern",
      "params": { "pattern": "[0-9]{5}" },
      "message": "should match pattern \"[0-9]{5}\""
    }
  ]
}

How have you encountered this problem? I'm not sure about custom keywords and other advanced (in a way) features, but in all of our cases it worked as it is.

@adrianschneider94
Copy link
Author

Yes I've had this problem.

Example that does not work

The issue only applies to arrays.

However, I was wrong with the root of the problem. I switched to jsonPointers: true in the ajv options unawares, while I was testing. This is why I got the slashes in the dataPath. (JsonPointers is required for ajv-errors which is a great addition.)

Without jsonPointers the error looks like this:

[
    {
        keyword: "const",
        dataPath: ".doesWork.testString",
        schemaPath: "#/properties/doesWork/properties/testString/const",
        params: {allowedValue: "test"},
        message: "should be equal to constant"
    },
    {
        keyword: "const",
        dataPath: ".test[0].doesNotWork", // uniforms would need ".test.0.doesNotWork", right?
        schemaPath: "#/properties/test/items/properties/doesNotWork/const",
        params: {allowedValue: "test"}
    }
]

@radekmie
Copy link
Contributor

Yep, brackets in the dataPath are not handled correctly. OK, got it, we'll work on it.

@kyleder
Copy link

kyleder commented Sep 17, 2019

I encountered this same issue and solved it by doing a string replace of the square brackets in the getError method:

master...kyleder:kd-fix-nested-inline-errors

I'm not sure if that's the best approach to fixing the issue, but it seems to solve it for my case.

@radekmie radekmie added Type: Bug Bug reports and their fixes and removed Type: Question Questions and other discussions labels Sep 18, 2019
radekmie added a commit that referenced this issue Sep 18, 2019
@Monteth Monteth added this to Closed in Open Source (migrated) Jun 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Bug Bug reports and their fixes
Projects
Archived in project
Development

No branches or pull requests

3 participants