Skip to content

Enable ajv version 6 with json schema draft-06 support#6

Open
jpettit wants to merge 1 commit intoemanualjade:masterfrom
jpettit:jpettit/ajv-draft-06
Open

Enable ajv version 6 with json schema draft-06 support#6
jpettit wants to merge 1 commit intoemanualjade:masterfrom
jpettit:jpettit/ajv-draft-06

Conversation

@jpettit
Copy link
Copy Markdown

@jpettit jpettit commented Oct 27, 2018

Specifying a const value is only supported in json-schema 6. Updating the schema draft enables validation of the following response body like so in a task:

# example json data: 
# { "field": "foobar" }

# task yml
...
  validate:
    status: 200
    body:
      properties:
        field:
          const: "foobar"

@jpettit
Copy link
Copy Markdown
Author

jpettit commented Oct 27, 2018

@ehmicky - any reservation to upgrading to 6?

@ehmicky
Copy link
Copy Markdown
Contributor

ehmicky commented Oct 27, 2018

I think that's a great idea. We can actually even go to JSON schema v7.

What works:

  • ajv, the library we use for validating the HTTP response with JSON schema, supports v7

What does not work:

  • OpenAPI 2.0/3.0 only uses JSON schema v4. Actually it's not even pure JSON schema, there are few minor differences (see openapi-schema-to-json-schema, a converter I contribute to, and used by test-openapi). JSON schema v4 is not forward compatible with v6/v7. For example exclusiveMaximum goes from being a boolean to being an integer. So we would need to convert JSON schema v4 to v7.
  • The $$random template helper and the HTTP request random generation based on JSON schema use json-schema-faker. Unfortunately they only support JSON schema v4 at the moment. I've opened many issues but it seems like v6/v7 support is hard and going to take a long time. The plan could be to allow JSON schema v7 in $$random but to convert it down to JSON schema v4.

For the v4 to v7 and v7 to v4 conversions, I am unfortunately not aware any library currently does that (if you do, please let me know!), which probably means creating that first. I actually am quite familiar with JSON schema (I went through the spec many times) and I have a list of all the conversions required, but this needs to be implemented and unit tested. That's the main issue.

By the way most JSON schema v7 can be expressed in JSON schema v4 (although much more verbosely). For example:

  • const: "foobar" -> enum: ["foobar"]
  • if, then, else -> oneOf + allOf + not
  • contains: value -> { not: { items: { not: value } } }

Actually the only JSON schema v7 property that (I think) cannot be converted to v4 is propertyNames.

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.

2 participants