Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

JSON Schema validation error when submitting a form with a date field

See original GitHub issue

Issue Description

Hi! I have a swagger file that looks like this:

      summary: Submit a purchase
      description: |
        Purchase form
        - application/json
        - name: body
          in: body
          required: true
            $ref: '#/definitions/Booking'
          description: Purchase successful
            $ref: '#/definitions/Success'
    type: object
        type: integer
        format: int32
        type: string
        format: date

Note the date field that has a format date. When I submit a POST request to /purchases with a body that looks like this:

      "tour_id": 1,
      "date": "2017-08-11"

I get this error:

JSON Schema validation error. 
Data path: "date" 
Schema path: "/items/properties/date/type" 
Invalid type: object (expected string)
Invalid type: object (expected string)
    at ono (/client/node_modules/ono/lib/index.js:62:17)
    at parseParameter (/client/node_modules/swagger-express-middleware/lib/param-parser.js:148:11)
    at /client/node_modules/swagger-express-middleware/lib/param-parser.js:93:18

Error: JSON Schema validation error. 
Data path: "date" 
Schema path: "/items/properties/date/type" 
Invalid type: object (expected string)
    at ono (/client/node_modules/ono/lib/index.js:62:17)
    at jsonValidate (/client/node_modules/swagger-express-middleware/lib/helpers/json-schema.js:208:11)
    at parseArray (/client/node_modules/swagger-express-middleware/lib/helpers/json-schema.js:435:5)
    at JsonSchema.parse (/client/node_modules/swagger-express-middleware/lib/helpers/json-schema.js:89:14)
    at /client/node_modules/swagger-express-middleware/lib/helpers/json-schema.js:513:42

I assume that this library converts the date field to a date object, and passes it to the JSON schema validator, which expects it to be string.

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:2
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

JamesMessingercommented, Jun 27, 2017

Ok, thanks for the clarification. I’ve updated my test code, and now I’m able to reproduce the bug. The problem boils down to this:

  1. The code successfully validates each object in the array
  2. It then converts the date strings to Date objects
  3. It then attempts to validate the array, but now the array is invalid because its objects don’t match the schema
quielacommented, Jun 25, 2019

I just ommited the date-time-format and pass strings. This is working for now. Not nice though.

Same issue, same not nice solution…

Read more comments on GitHub >

github_iconTop Results From Across the Web

Validation - react-jsonschema-form documentation
React Json Schema Form provides a default @rjsf/validator-ajv6 ... errors (such as when a field is required ) may be displayed before the...
Read more >
Json schema validation error - Stack Overflow
This errors happen when validating the JSON with the schema draft-04 or higher, the problem is that "id" is expected to be in...
Read more >
Validation - JSON Forms
The easiest way to declare an error for a property is by specifying the instancePath , which represents the path of the property...
Read more >
Date format in JSON schema - Forums - IBM Support
Dates in YYYY-MM-DD format can be validated with format "date" as you thought. In particular, DataPower v6.0.0 will validate the string with this...
Read more >
A Vocabulary for Structural Validation of JSON - JSON Schema
This Internet-Draft is submitted in full conformance with the provisions of BCP 78 and BCP 79.¶ ... Date and time format names are...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Post

No results found

github_iconTop Related Hashnode Post

No results found