Skip to content

Commit 2cc6d1f

Browse files
committed
refactor(rules): two use cases not handled by current implementation
1 parent 8becb8a commit 2cc6d1f

5 files changed

Lines changed: 405 additions & 15 deletions

File tree

.changeset/nasty-peas-agree.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@redocly/openapi-core': patch
3+
'@redocly/cli': patch
4+
---
5+
6+
Updated `no-required-schema-properties-undefined` rule to cover additional edge cases.

docs/@v1/rules/oas/no-required-schema-properties-undefined.md

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,86 @@ schemas:
9898
- Name
9999
```
100100

101+
The rule accepts bare `required` constraints on property sub-schemas when the property's type is defined in a parent `allOf` sibling. This is a valid JSON Schema pattern for adding presence constraints on top of a referenced base type:
102+
103+
```yaml
104+
schemas:
105+
PersonBase:
106+
type: object
107+
properties:
108+
personName:
109+
type: object
110+
properties:
111+
givenName:
112+
type: string
113+
familyName:
114+
type: string
115+
Person:
116+
type: object
117+
allOf:
118+
- $ref: '#/components/schemas/PersonBase'
119+
properties:
120+
personName:
121+
required:
122+
- givenName
123+
- familyName
124+
required:
125+
- personName
126+
```
127+
128+
The rule also accepts `oneOf` branches used as pure constraint fragments, where each branch contains only a `required` keyword and the property's type is defined in a parent `allOf` sibling:
129+
130+
```yaml
131+
schemas:
132+
PersonBase:
133+
type: object
134+
properties:
135+
communication:
136+
type: object
137+
properties:
138+
landlines:
139+
type: array
140+
mobiles:
141+
type: array
142+
emails:
143+
type: array
144+
Person:
145+
type: object
146+
allOf:
147+
- $ref: '#/components/schemas/PersonBase'
148+
properties:
149+
communication:
150+
oneOf:
151+
- required:
152+
- landlines
153+
- required:
154+
- mobiles
155+
- required:
156+
- emails
157+
required:
158+
- communication
159+
```
160+
161+
Misspellings in bare `required` lists are still caught. If a required key does not exist in the property's type definition resolved through the parent `allOf` sibling, the rule reports an error:
162+
163+
```yaml
164+
schemas:
165+
Person:
166+
type: object
167+
allOf:
168+
- $ref: '#/components/schemas/PersonBase'
169+
properties:
170+
personName:
171+
required:
172+
- giveName # misspelling of givenName
173+
required:
174+
- personName
175+
```
176+
177+
```bash
178+
Required property 'giveName' is undefined.
179+
```
180+
101181
## Related rules
102182

103183
- [no-invalid-schema-examples](./no-invalid-schema-examples.md)

docs/@v2/rules/common/no-required-schema-properties-undefined.md

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,86 @@ schemas:
128128
example: doggie
129129
```
130130

131+
The rule accepts bare `required` constraints on property sub-schemas when the property's type is defined in a parent `allOf` sibling. This is a valid JSON Schema pattern for adding presence constraints on top of a referenced base type:
132+
133+
```yaml
134+
schemas:
135+
PersonBase:
136+
type: object
137+
properties:
138+
personName:
139+
type: object
140+
properties:
141+
givenName:
142+
type: string
143+
familyName:
144+
type: string
145+
Person:
146+
type: object
147+
allOf:
148+
- $ref: '#/components/schemas/PersonBase'
149+
properties:
150+
personName:
151+
required:
152+
- givenName
153+
- familyName
154+
required:
155+
- personName
156+
```
157+
158+
The rule also accepts `oneOf` branches used as pure constraint fragments, where each branch contains only a `required` keyword and the property's type is defined in a parent `allOf` sibling:
159+
160+
```yaml
161+
schemas:
162+
PersonBase:
163+
type: object
164+
properties:
165+
communication:
166+
type: object
167+
properties:
168+
landlines:
169+
type: array
170+
mobiles:
171+
type: array
172+
emails:
173+
type: array
174+
Person:
175+
type: object
176+
allOf:
177+
- $ref: '#/components/schemas/PersonBase'
178+
properties:
179+
communication:
180+
oneOf:
181+
- required:
182+
- landlines
183+
- required:
184+
- mobiles
185+
- required:
186+
- emails
187+
required:
188+
- communication
189+
```
190+
191+
Misspellings in bare `required` lists are still caught. If a required key does not exist in the property's type definition resolved through the parent `allOf` sibling, the rule reports an error:
192+
193+
```yaml
194+
schemas:
195+
Person:
196+
type: object
197+
allOf:
198+
- $ref: '#/components/schemas/PersonBase'
199+
properties:
200+
personName:
201+
required:
202+
- giveName # misspelling of givenName
203+
required:
204+
- personName
205+
```
206+
207+
```bash
208+
Required property 'giveName' is undefined.
209+
```
210+
131211
## Related rules
132212

133213
- [no-schema-type-mismatch](./no-schema-type-mismatch.md)

0 commit comments

Comments
 (0)