Skip to content

Commit f5d6efa

Browse files
authored
feat: add format validation (#2172)
1 parent 2d00ab8 commit f5d6efa

File tree

8 files changed

+348
-3
lines changed

8 files changed

+348
-3
lines changed

.changeset/cyan-months-visit.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@redocly/openapi-core": minor
3+
"@redocly/cli": minor
4+
---
5+
6+
Added validation for JSON Schema format.
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
openapi: 3.1.0
2+
info:
3+
title: Test format validation
4+
version: 1.0.0
5+
paths:
6+
/test:
7+
get:
8+
responses:
9+
200:
10+
description: Validate formats using $refs.
11+
content:
12+
application/json:
13+
schema:
14+
type: string
15+
format: date-time
16+
examples:
17+
Correct:
18+
$ref: '#/components/examples/DateTime'
19+
Incorrect:
20+
$ref: '#/components/examples/Date'
21+
22+
components:
23+
schemas:
24+
Date:
25+
type: string
26+
format: date
27+
examples:
28+
- '2000-01-01' # correct
29+
- 2000 # incorrect type
30+
- incorrect
31+
Email:
32+
description: Email address for ticket purchaser.
33+
type: string
34+
format: email
35+
examples:
36+
- [email protected] # correct
37+
- wrong.format
38+
Id:
39+
type: string
40+
format: uuid
41+
examples:
42+
- 3be6453c-03eb-4357-ae5a-984a0e574a54 # correct
43+
- incorrect
44+
- 42 # wrong type
45+
Time:
46+
type: string
47+
pattern: '^([01]\d|2[0-3]):?([0-5]\d)$'
48+
description: Time the museum opens on a specific date. Uses 24 hour time format (`HH:mm`).
49+
examples:
50+
- 09:00 # correct
51+
- incorrect
52+
- 09.00 # wrong type
53+
Oneof:
54+
type: string
55+
oneOf:
56+
- format: date
57+
- pattern: ^(month|year)ly$
58+
examples:
59+
- '2000-01-01' # correct
60+
- monthly # correct
61+
- wrong
62+
63+
examples:
64+
Date:
65+
value: '2000-01-01'
66+
DateTime:
67+
value: '2000-01-01T12:00:00Z'
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
apis:
2+
main:
3+
root: openapi.yaml
4+
5+
rules:
6+
no-invalid-parameter-examples: error
7+
no-invalid-media-type-examples: error
8+
no-invalid-schema-examples: error
9+
no-unresolved-refs: error
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
[1] openapi.yaml:65:14 at #/components/examples/Date/value
2+
3+
Example value must conform to the schema: must match format "date-time".
4+
5+
63 | examples:
6+
64 | Date:
7+
65 | value: '2000-01-01'
8+
| ^^^^^^^^^^^^
9+
66 | DateTime:
10+
67 | value: '2000-01-01T12:00:00Z'
11+
12+
referenced from openapi.yaml:13:15 at #/paths/~1test/get/responses/200/content/application~1json
13+
14+
Error was generated by the no-invalid-media-type-examples rule.
15+
16+
17+
[2] openapi.yaml:29:11 at #/components/schemas/Date/examples/1
18+
19+
Example value must conform to the schema: type must be string.
20+
21+
27 | examples:
22+
28 | - '2000-01-01' # correct
23+
29 | - 2000 # incorrect type
24+
| ^^^^
25+
30 | - incorrect
26+
31 | Email:
27+
28+
referenced from openapi.yaml:25:7 at #/components/schemas/Date
29+
30+
Error was generated by the no-invalid-schema-examples rule.
31+
32+
33+
[3] openapi.yaml:30:11 at #/components/schemas/Date/examples/2
34+
35+
Example value must conform to the schema: must match format "date".
36+
37+
28 | - '2000-01-01' # correct
38+
29 | - 2000 # incorrect type
39+
30 | - incorrect
40+
| ^^^^^^^^^
41+
31 | Email:
42+
32 | description: Email address for ticket purchaser.
43+
44+
referenced from openapi.yaml:25:7 at #/components/schemas/Date
45+
46+
Error was generated by the no-invalid-schema-examples rule.
47+
48+
49+
[4] openapi.yaml:37:11 at #/components/schemas/Email/examples/1
50+
51+
Example value must conform to the schema: must match format "email".
52+
53+
35 | examples:
54+
36 | - [email protected] # correct
55+
37 | - wrong.format
56+
| ^^^^^^^^^^^^
57+
38 | Id:
58+
39 | type: string
59+
60+
referenced from openapi.yaml:32:7 at #/components/schemas/Email
61+
62+
Error was generated by the no-invalid-schema-examples rule.
63+
64+
65+
[5] openapi.yaml:43:11 at #/components/schemas/Id/examples/1
66+
67+
Example value must conform to the schema: must match format "uuid".
68+
69+
41 | examples:
70+
42 | - 3be6453c-03eb-4357-ae5a-984a0e574a54 # correct
71+
43 | - incorrect
72+
| ^^^^^^^^^
73+
44 | - 42 # wrong type
74+
45 | Time:
75+
76+
referenced from openapi.yaml:39:7 at #/components/schemas/Id
77+
78+
Error was generated by the no-invalid-schema-examples rule.
79+
80+
81+
[6] openapi.yaml:44:11 at #/components/schemas/Id/examples/2
82+
83+
Example value must conform to the schema: type must be string.
84+
85+
42 | - 3be6453c-03eb-4357-ae5a-984a0e574a54 # correct
86+
43 | - incorrect
87+
44 | - 42 # wrong type
88+
| ^^
89+
45 | Time:
90+
46 | type: string
91+
92+
referenced from openapi.yaml:39:7 at #/components/schemas/Id
93+
94+
Error was generated by the no-invalid-schema-examples rule.
95+
96+
97+
[7] openapi.yaml:51:11 at #/components/schemas/Time/examples/1
98+
99+
Example value must conform to the schema: must match pattern "^([01]\d|2[0-3]):?([0-5]\d)$".
100+
101+
49 | examples:
102+
50 | - 09:00 # correct
103+
51 | - incorrect
104+
| ^^^^^^^^^
105+
52 | - 09.00 # wrong type
106+
53 | Oneof:
107+
108+
referenced from openapi.yaml:46:7 at #/components/schemas/Time
109+
110+
Error was generated by the no-invalid-schema-examples rule.
111+
112+
113+
[8] openapi.yaml:52:11 at #/components/schemas/Time/examples/2
114+
115+
Example value must conform to the schema: type must be string.
116+
117+
50 | - 09:00 # correct
118+
51 | - incorrect
119+
52 | - 09.00 # wrong type
120+
| ^^^^^
121+
53 | Oneof:
122+
54 | type: string
123+
124+
referenced from openapi.yaml:46:7 at #/components/schemas/Time
125+
126+
Error was generated by the no-invalid-schema-examples rule.
127+
128+
129+
[9] openapi.yaml:61:11 at #/components/schemas/Oneof/examples/2
130+
131+
Example value must conform to the schema: must match format "date".
132+
133+
59 | - '2000-01-01' # correct
134+
60 | - monthly # correct
135+
61 | - wrong
136+
| ^^^^^
137+
62 |
138+
63 | examples:
139+
140+
referenced from openapi.yaml:54:7 at #/components/schemas/Oneof
141+
142+
Error was generated by the no-invalid-schema-examples rule.
143+
144+
145+
[10] openapi.yaml:61:11 at #/components/schemas/Oneof/examples/2
146+
147+
Example value must conform to the schema: must match pattern "^(month|year)ly$".
148+
149+
59 | - '2000-01-01' # correct
150+
60 | - monthly # correct
151+
61 | - wrong
152+
| ^^^^^
153+
62 |
154+
63 | examples:
155+
156+
referenced from openapi.yaml:54:7 at #/components/schemas/Oneof
157+
158+
Error was generated by the no-invalid-schema-examples rule.
159+
160+
161+
[11] openapi.yaml:61:11 at #/components/schemas/Oneof/examples/2
162+
163+
Example value must conform to the schema: must match exactly one schema in oneOf.
164+
165+
59 | - '2000-01-01' # correct
166+
60 | - monthly # correct
167+
61 | - wrong
168+
| ^^^^^
169+
62 |
170+
63 | examples:
171+
172+
referenced from openapi.yaml:54:7 at #/components/schemas/Oneof
173+
174+
Error was generated by the no-invalid-schema-examples rule.
175+
176+
177+
178+
validating openapi.yaml...
179+
openapi.yaml: validated in <test>ms
180+
181+
❌ Validation failed with 11 errors.
182+
run `redocly lint --generate-ignore-file` to add all problems to the ignore file.
183+

package-lock.json

Lines changed: 77 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/core/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
"dependencies": {
6060
"@redocly/ajv": "^8.11.2",
6161
"@redocly/config": "^0.26.3",
62+
"ajv-formats": "^2.1.1",
6263
"colorette": "^1.2.0",
6364
"js-levenshtein": "^1.1.6",
6465
"js-yaml": "^4.1.0",

0 commit comments

Comments
 (0)