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

Generate pretty errors for validation errors #19

Merged
merged 2 commits into from
Aug 18, 2017
Merged

Conversation

dgmora
Copy link
Contributor

@dgmora dgmora commented Aug 18, 2017

Instead of

     Blumquist::Errors::ValidationError:
       [["The property '#/name' of type integer did not match the following type: string in schema 658659b8-41dc-5211-
9cb4-0912a8ab1cc4"],{"$schema":"http://json-schema.org/draft-04/schema#","definitions":{"address":{"type":"object","pr
operties":{"street_address":{"type":"string"},"city":{"type":"string"},"state":{"type":["string","null"]}},"required":
["street_address","city","state"]},"sibling":{"type":"object","properties":{"age_difference":{"type":"number"},"name":
{"type":"string"}},"required":["age_difference"]},"ancestor":{"type":"object","properties":{"family_name":{"type":"str
ing"}},"required":["family_name"]}},"type":"object","properties":{"name":{"type":"string","maxLength":16},"phone_numbe
rs":{"type":"array","items":[{"type":"object","properties":{"prefix":{"type":"number"},"extension":{"type":"number"}}}
]},"parents_address":{"$ref":"#/definitions/address"},"current_address":{"oneOf":[{"type":"null"},{"$ref":"#/definitio
ns/address"},{"type":"object","properties":{"planet":{"type":"string"}},"required":["planet"]}]},"relatives":{"type":"
array","items":{"oneOf":[{"$ref":"#/definitions/sibling"},{"$ref":"#/definitions/ancestor"}]}},"old_addresses":{"type"
:"array","items":[{"$ref":"#/definitions/address"}]}}},{"name":1,"phone_numbers":[{"prefix":555,"extension":1234}],"cu
rrent_address":{"street_address":"Chausseestr. 111","city":"Berlin","state":"Berlin"},"old_addresses":[{"street_addres
s":"Friedrichstr. 58","city":"Berlin","state":"Berlin"},{"street_address":"Mehringdamm 33","city":"Berlin","state":"Be
rlin"},{"street_address":"Bluecherstr. 22","city":"Berlin","state":"Berlin"}]}]

You see:

Blumquist::Errors::ValidationError:
  [
    [
      "The property '#/name' of type integer did not match the following type: string in schema 658659b8-41dc-5211-9cb4-0912a8ab1cc4"
    ],
    {
      "name": 1,
      "phone_numbers": [
        {
          "prefix": 555,
          "extension": 1234
        }
      ],
      "current_address": {
        "street_address": "Chausseestr. 111",
        "city": "Berlin",
        "state": "Berlin"
      },
      "old_addresses": [
        {
          "street_address": "Friedrichstr. 58",
          "city": "Berlin",
          "state": "Berlin"
        },
        {
          "street_address": "Mehringdamm 33",
          "city": "Berlin",
          "state": "Berlin"
        },
        {
          "street_address": "Bluecherstr. 22",
          "city": "Berlin",
          "state": "Berlin"
        }
      ]
    },
    {
      "$schema": "http://json-schema.org/draft-04/schema#",
      "definitions": {
        "address": {
          "type": "object",
          "properties": {
            "street_address": {
              "type": "string"
            },
            "city": {
              "type": "string"
            },
            "state": {
              "type": [
                "string",
                "null"
              ]
            }
          },
          "required": [
            "street_address",
            "city",
            "state"
          ]
        },
        "sibling": {
          "type": "object",
          "properties": {
            "age_difference": {
              "type": "number"
            },
            "name": {
              "type": "string"
            }
          },
          "required": [
            "age_difference"
          ]
        },
        "ancestor": {
          "type": "object",
          "properties": {
            "family_name": {
              "type": "string"
            }
          },
          "required": [
            "family_name"
          ]
        }
      },
      "type": "object",
      "properties": {
        "name": {
          "type": "string",
          "maxLength": 16
        },
        "phone_numbers": {
          "type": "array",
          "items": [
            {
              "type": "object",
              "properties": {
                "prefix": {
                  "type": "number"
                },
                "extension": {
                  "type": "number"
                }
              }
            }
          ]
        },
        "parents_address": {
          "$ref": "#/definitions/address"
        },
        "current_address": {
          "oneOf": [
            {
              "type": "null"
            },
            {
              "$ref": "#/definitions/address"
            },
            {
              "type": "object",
              "properties": {
                "planet": {
                  "type": "string"
                }
              },
              "required": [
                "planet"
              ]
            }
          ]
        },
        "relatives": {
          "type": "array",
          "items": {
            "oneOf": [
              {
                "$ref": "#/definitions/sibling"
              },
              {
                "$ref": "#/definitions/ancestor"
              }
            ]
          }
        },
        "old_addresses": {
          "type": "array",
          "items": [
            {
              "$ref": "#/definitions/address"
            }
          ]
        }
      }
    }
  ]

It is quite long but it's at least readable. Do you think we could remove the schema from the exception? The schema should be available for the user somewhere, but then you can't find the error by just looking at the exception.

@jayniz
Copy link
Owner

jayniz commented Aug 18, 2017

Yeah, out with the schema!

@jayniz jayniz assigned dgmora and unassigned jayniz Aug 18, 2017
@coveralls
Copy link

Coverage Status

Coverage remained the same at 88.108% when pulling 89a9571 on prettier-errors into fb3d650 on master.

@dgmora
Copy link
Contributor Author

dgmora commented Aug 18, 2017

@jayniz I had to add this because when there were errors in arrays you'd get:

         "The property '#/foo/0' of type object matched more than one of the required schemas. The schema
 specific errors were:\n\n- oneOf #0:\n    - The property '#/foo/0' did not contain a required property o
f 'title'\n- oneOf #3:\n    - The property '#/foo/0' did not contain a required property of 'other prop'\n- The property
 '#/foo/1' did not contain a required property of 'title'\n- oneOf #3:\n    - The property '#/foo/1'
did not contain a required property of 'other prop'\n
...

As the \n are inside a string, those get unscaped when transformed to json.
To avoid that, I transformed the errors from strings to arrays. It looks a bit better:

           [
             "The property '#/foo/0' of type object matched more than one of the required schemas. The sc
hema specific errors were:",
             "",
             "- oneOf #0:",
             "    - The property '#/foo/0' did not contain a required property of 'title'",
             "- oneOf #3:",
             "    - The property '#/foo/0' did not contain a required property of 'other prop'",

At some point I think we could dump all the json errors part.

@dgmora dgmora merged commit 5c6ea8d into master Aug 18, 2017
@dgmora dgmora deleted the prettier-errors branch August 18, 2017 14:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants