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

feat: add format validation #1820

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/angry-taxis-share.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@redocly/openapi-core": minor
"@redocly/cli": minor
---

Added validation for JSON Schema format.
67 changes: 67 additions & 0 deletions __tests__/lint/validate-schema-formats/openapi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
openapi: 3.1.0
info:
title: Test format validation
version: 1.0.0
paths:
/test:
get:
responses:
200:
description: Validate formats using $refs.
content:
application/json:
schema:
type: string
format: date-time
examples:
Correct:
$ref: '#/components/examples/DateTime'
Incorrect:
$ref: '#/components/examples/Date'

components:
schemas:
Date:
type: string
format: date
examples:
- '2000-01-01' # correct
- 2000 # incorrect type
- incorrect
Email:
description: Email address for ticket purchaser.
type: string
format: email
examples:
- [email protected] # correct
- wrong.format
Id:
type: string
format: uuid
examples:
- 3be6453c-03eb-4357-ae5a-984a0e574a54 # correct
- incorrect
- 42 # wrong type
Time:
type: string
pattern: '^([01]\d|2[0-3]):?([0-5]\d)$'
description: Time the museum opens on a specific date. Uses 24 hour time format (`HH:mm`).
examples:
- 09:00 # correct
- incorrect
- 09.00 # wrong type
Oneof:
type: string
oneOf:
- format: date
- pattern: ^(month|year)ly$
examples:
- '2000-01-01' # correct
- monthly # correct
- wrong

examples:
Date:
value: '2000-01-01'
DateTime:
value: '2000-01-01T12:00:00Z'
9 changes: 9 additions & 0 deletions __tests__/lint/validate-schema-formats/redocly.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
apis:
main:
root: openapi.yaml

rules:
no-invalid-parameter-examples: error
no-invalid-media-type-examples: error
no-invalid-schema-examples: error
no-unresolved-refs: error
188 changes: 188 additions & 0 deletions __tests__/lint/validate-schema-formats/snapshot.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`E2E lint validate-schema-formats 1`] = `

validating openapi.yaml...
[1] openapi.yaml:65:14 at #/components/examples/Date/value

Example value must conform to the schema: must match format "date-time".

63 | examples:
64 | Date:
65 | value: '2000-01-01'
| ^^^^^^^^^^^^
66 | DateTime:
67 | value: '2000-01-01T12:00:00Z'

referenced from openapi.yaml:13:15 at #/paths/~1test/get/responses/200/content/application~1json

Error was generated by the no-invalid-media-type-examples rule.


[2] openapi.yaml:29:11 at #/components/schemas/Date/examples/1

Example value must conform to the schema: type must be string.

27 | examples:
28 | - '2000-01-01' # correct
29 | - 2000 # incorrect type
| ^^^^
30 | - incorrect
31 | Email:

referenced from openapi.yaml:25:7 at #/components/schemas/Date

Error was generated by the no-invalid-schema-examples rule.


[3] openapi.yaml:30:11 at #/components/schemas/Date/examples/2

Example value must conform to the schema: must match format "date".

28 | - '2000-01-01' # correct
29 | - 2000 # incorrect type
30 | - incorrect
| ^^^^^^^^^
31 | Email:
32 | description: Email address for ticket purchaser.

referenced from openapi.yaml:25:7 at #/components/schemas/Date

Error was generated by the no-invalid-schema-examples rule.


[4] openapi.yaml:37:11 at #/components/schemas/Email/examples/1

Example value must conform to the schema: must match format "email".

35 | examples:
36 | - [email protected] # correct
37 | - wrong.format
| ^^^^^^^^^^^^
38 | Id:
39 | type: string

referenced from openapi.yaml:32:7 at #/components/schemas/Email

Error was generated by the no-invalid-schema-examples rule.


[5] openapi.yaml:43:11 at #/components/schemas/Id/examples/1

Example value must conform to the schema: must match format "uuid".

41 | examples:
42 | - 3be6453c-03eb-4357-ae5a-984a0e574a54 # correct
43 | - incorrect
| ^^^^^^^^^
44 | - 42 # wrong type
45 | Time:

referenced from openapi.yaml:39:7 at #/components/schemas/Id

Error was generated by the no-invalid-schema-examples rule.


[6] openapi.yaml:44:11 at #/components/schemas/Id/examples/2

Example value must conform to the schema: type must be string.

42 | - 3be6453c-03eb-4357-ae5a-984a0e574a54 # correct
43 | - incorrect
44 | - 42 # wrong type
| ^^
45 | Time:
46 | type: string

referenced from openapi.yaml:39:7 at #/components/schemas/Id

Error was generated by the no-invalid-schema-examples rule.


[7] openapi.yaml:51:11 at #/components/schemas/Time/examples/1

Example value must conform to the schema: must match pattern "^([01]\\d|2[0-3]):?([0-5]\\d)$".

49 | examples:
50 | - 09:00 # correct
51 | - incorrect
| ^^^^^^^^^
52 | - 09.00 # wrong type
53 | Oneof:

referenced from openapi.yaml:46:7 at #/components/schemas/Time

Error was generated by the no-invalid-schema-examples rule.


[8] openapi.yaml:52:11 at #/components/schemas/Time/examples/2

Example value must conform to the schema: type must be string.

50 | - 09:00 # correct
51 | - incorrect
52 | - 09.00 # wrong type
| ^^^^^
53 | Oneof:
54 | type: string

referenced from openapi.yaml:46:7 at #/components/schemas/Time

Error was generated by the no-invalid-schema-examples rule.


[9] openapi.yaml:61:11 at #/components/schemas/Oneof/examples/2

Example value must conform to the schema: must match format "date".

59 | - '2000-01-01' # correct
60 | - monthly # correct
61 | - wrong
| ^^^^^
62 |
63 | examples:

referenced from openapi.yaml:54:7 at #/components/schemas/Oneof

Error was generated by the no-invalid-schema-examples rule.


[10] openapi.yaml:61:11 at #/components/schemas/Oneof/examples/2

Example value must conform to the schema: must match pattern "^(month|year)ly$".

59 | - '2000-01-01' # correct
60 | - monthly # correct
61 | - wrong
| ^^^^^
62 |
63 | examples:

referenced from openapi.yaml:54:7 at #/components/schemas/Oneof

Error was generated by the no-invalid-schema-examples rule.


[11] openapi.yaml:61:11 at #/components/schemas/Oneof/examples/2

Example value must conform to the schema: must match exactly one schema in oneOf.

59 | - '2000-01-01' # correct
60 | - monthly # correct
61 | - wrong
| ^^^^^
62 |
63 | examples:

referenced from openapi.yaml:54:7 at #/components/schemas/Oneof

Error was generated by the no-invalid-schema-examples rule.


openapi.yaml: validated in <test>ms

❌ Validation failed with 11 errors.
run \`redocly lint --generate-ignore-file\` to add all problems to the ignore file.


`;
77 changes: 77 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading