Skip to content

Commit 88bb422

Browse files
committed
update extension keywords; implement string formats and modifiers in x-types-adapter; implement $descirptions in json-schema-adapter
1 parent 2eeb10b commit 88bb422

15 files changed

+2719
-101
lines changed

README.md

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,21 @@ Any [valid JSON](https://www.json.org/) could be validated against a **JSON X-Ty
55

66
## Reserved Keywords
77

8-
| Keyword | Description | Usage |
9-
| ------------- | ------------------------------------------------------------------------- | ---------- |
10-
| string | String type. | key, value |
11-
| number | Number type. | value |
12-
| boolean | Boolean type. | value |
13-
| `null` | The `null` value. The string "null" value doesn't have a special meaning. | value |
14-
| undefined | Value is not set (the corresponding key is not present). | value |
15-
| array | Array generic. | key |
16-
| any | Any value (not validated). | value |
17-
| $and | Refers to the intersection of an array members ([🔗](#types-combining)). | key |
18-
| $descriptions | Object with descriptions of the fields at the same level. | key |
19-
| $ref | Reference to another **JSON X-Type** ([🔗](#references)). | key |
20-
| $schema | A literal JSON Schema definition ([🔗](#literal-schemas)). | key |
8+
| Keyword | Description | Usage |
9+
| --------- | ------------------------------------------------------------------------------------- | ---------- |
10+
| string | String type. | key, value |
11+
| number | Number type. | value |
12+
| boolean | Boolean type. | value |
13+
| `null` | The `null` value. (Note that the string "null" value doesn't have a special meaning.) | value |
14+
| undefined | Value is not set (the corresponding key is not present). | value |
15+
| array | Array generic. | key |
16+
| any | Any value (not validated). | value |
17+
| $and | Refers to the intersection of an array members ([🔗](#types-combining)). | key |
18+
| $ref | Reference to another **JSON X-Type** ([🔗](#references)). | key |
19+
| $schema | A literal JSON Schema definition ([🔗](#literal-schemas)). | key |
2120

2221
The list could be extended with other `$`-prefixed keywords.
23-
So it's a good idea to escape any values that start with `$` using the `$literal` prefix.
24-
25-
## Prefixes
26-
27-
Prefixes are used to modify what's going after them. Prefixes and the actual values are separated by a colon.
28-
29-
| Prefix | Description | Usage |
30-
| -------- | --------------------------------------------------- | ---------- |
31-
| $literal | Escapes a literal value ([🔗](#literals-escaping)). | key, value |
22+
So it's a good idea to escape any values that start with `$` using the `$literal` prefix ([🔗](#literals-escaping)).
3223

3324
## Objects
3425

@@ -118,7 +109,7 @@ Impossible combinations should result in the `undefined` type.
118109
119110
## Literals Escaping
120111
121-
Whenever there is a need to use a literal string value instead of a reserved keyword, it needs to be prepended with the `$literal` prefix:
112+
Whenever there is a need to use a literal string value instead of a reserved keyword, it must be prepended with the `$literal:` prefix:
122113
123114
```json
124115
{

applications/__tests__/__snapshots__/e2e.test.js.snap

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ paths:
7676
- required
7777
additionalProperties:
7878
type: boolean
79+
patternProperties: {}
7980
Ref:
8081
type: number
8182
RefObject:
@@ -97,6 +98,7 @@ paths:
9798
- somekey
9899
- string
99100
additionalProperties: false
101+
patternProperties: {}
100102
ConditionalRef:
101103
type: object
102104
properties:
@@ -116,6 +118,7 @@ paths:
116118
- somekey
117119
- string
118120
additionalProperties: false
121+
patternProperties: {}
119122
required:
120123
- Simple
121124
- Or
@@ -124,6 +127,7 @@ paths:
124127
- Ref
125128
- RefObject
126129
additionalProperties: false
130+
patternProperties: {}
127131
components:
128132
x-types:
129133
Foo: number
@@ -198,6 +202,7 @@ paths:
198202
- string
199203
additionalProperties:
200204
type: boolean
205+
patternProperties: {}
201206
description: An array of boolean records with the fixed 'string' property.
202207
Conditional:
203208
type: string
@@ -221,6 +226,7 @@ paths:
221226
required:
222227
- ReferencedAgain
223228
additionalProperties: false
229+
patternProperties: {}
224230
description: A reference with nested references.
225231
required:
226232
- Plain
@@ -229,6 +235,7 @@ paths:
229235
- Referenced
230236
- NestedReferenced
231237
additionalProperties: false
238+
patternProperties: {}
232239
components:
233240
x-types:
234241
Referenced: string
@@ -277,6 +284,7 @@ paths:
277284
- az
278285
- vidh
279286
additionalProperties: false
287+
patternProperties: {}
280288
- type: object
281289
properties:
282290
bukh:
@@ -287,6 +295,7 @@ paths:
287295
- bukh
288296
- vidh
289297
additionalProperties: false
298+
patternProperties: {}
290299
components: {}
291300
"
292301
`;
@@ -365,6 +374,7 @@ paths:
365374
- WrongSchemaFormat
366375
- Referenced
367376
additionalProperties: false
377+
patternProperties: {}
368378
components:
369379
schemas:
370380
Foo:
@@ -444,6 +454,7 @@ paths:
444454
- foo
445455
- bar
446456
additionalProperties: false
457+
patternProperties: {}
447458
application/problems+json:
448459
x-type:
449460
WrongAndFormat:
@@ -470,6 +481,7 @@ paths:
470481
- Empty
471482
- ImpossibleArraysCombination
472483
additionalProperties: false
484+
patternProperties: {}
473485
components:
474486
x-types:
475487
CorrectAnd:
@@ -526,6 +538,7 @@ paths:
526538
- AFieldWithRegularRef
527539
- AFieldWithInlineRef
528540
additionalProperties: false
541+
patternProperties: {}
529542
'201':
530543
description: A bare ref
531544
content:
@@ -599,6 +612,7 @@ paths:
599612
required:
600613
- AWrongRef
601614
additionalProperties: false
615+
patternProperties: {}
602616
components:
603617
x-types:
604618
Foo: any
@@ -667,9 +681,11 @@ paths:
667681
- Number
668682
- Integer
669683
additionalProperties: false
684+
patternProperties: {}
670685
required:
671686
- Formats
672687
additionalProperties: false
688+
patternProperties: {}
673689
components: {}
674690
"
675691
`;
@@ -783,10 +799,12 @@ paths:
783799
required:
784800
- bukh
785801
additionalProperties: false
802+
patternProperties: {}
786803
required:
787804
- Or
788805
- ComplexOr
789806
additionalProperties: false
807+
patternProperties: {}
790808
components:
791809
x-types:
792810
Or:
@@ -964,48 +982,48 @@ referenced from applications/outputs/x-openapi-with-refs.yaml:13:15 at #/paths/~
964982
Error was generated by the no-invalid-media-type-examples rule.
965983
966984
967-
[4] applications/outputs/x-openapi-with-refs.yaml:72:26 at #/paths/~1test/get/responses/202/content/application~1json/examples/Incorrect/value
985+
[4] applications/outputs/x-openapi-with-refs.yaml:73:26 at #/paths/~1test/get/responses/202/content/application~1json/examples/Incorrect/value
968986
969987
Example value must conform to the schema: type must be number.
970988
971-
70 | value: 42
972-
71 | Incorrect:
973-
72 | value: false
989+
71 | value: 42
990+
72 | Incorrect:
991+
73 | value: false
974992
| ^^^^^
975-
73 | schema:
976-
74 | type: number
993+
74 | schema:
994+
75 | type: number
977995
978-
referenced from applications/outputs/x-openapi-with-refs.yaml:67:15 at #/paths/~1test/get/responses/202/content/application~1json
996+
referenced from applications/outputs/x-openapi-with-refs.yaml:68:15 at #/paths/~1test/get/responses/202/content/application~1json
979997
980998
Error was generated by the no-invalid-media-type-examples rule.
981999
9821000
983-
[5] applications/outputs/x-openapi-with-refs.yaml:86:26 at #/paths/~1test/get/responses/203/content/application~1json/examples/Incorrect/value
1001+
[5] applications/outputs/x-openapi-with-refs.yaml:87:26 at #/paths/~1test/get/responses/203/content/application~1json/examples/Incorrect/value
9841002
9851003
Example value must conform to the schema: type must be array.
9861004
987-
84 | - anything
988-
85 | Incorrect:
989-
86 | value: anything
1005+
85 | - anything
1006+
86 | Incorrect:
1007+
87 | value: anything
9901008
| ^^^^^^^^
991-
87 | schema:
992-
88 | type: array
1009+
88 | schema:
1010+
89 | type: array
9931011
994-
referenced from applications/outputs/x-openapi-with-refs.yaml:79:15 at #/paths/~1test/get/responses/203/content/application~1json
1012+
referenced from applications/outputs/x-openapi-with-refs.yaml:80:15 at #/paths/~1test/get/responses/203/content/application~1json
9951013
9961014
Error was generated by the no-invalid-media-type-examples rule.
9971015
9981016
999-
[6] applications/outputs/x-openapi-with-refs.yaml:103:19 at #/paths/~1test/get/responses/404/content/application~1json/x-type/AWrongRef
1017+
[6] applications/outputs/x-openapi-with-refs.yaml:104:19 at #/paths/~1test/get/responses/404/content/application~1json/x-type/AWrongRef
10001018
10011019
Can't resolve $ref: ENOENT: no such file or directory './applications/outputs/wrong-file.yaml'
10021020
1003-
101 | x-type:
1004-
102 | AWrongRef:
1005-
103 | $ref: wrong-file.yaml
1021+
102 | x-type:
1022+
103 | AWrongRef:
1023+
104 | $ref: wrong-file.yaml
10061024
| ^^^^^^^^^^^^^^^^^^^^^
1007-
104 | example:
1008-
105 | AWrongRef: Accepts anything
1025+
105 | example:
1026+
106 | AWrongRef: Accepts anything
10091027
10101028
Error was generated by the no-unresolved-refs rule.
10111029

applications/__tests__/adapter.test.js

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,23 @@ describe('adapter', () => {
66
expect(translateXTypeToSchema('string')).toEqual({type: 'string'})
77
})
88

9-
test('translates literals', () => {
9+
test('literals', () => {
1010
expect(
1111
translateXTypeToSchema({'$literal:string': '$literal:boolean'})
1212
).toEqual({
1313
type: 'object',
1414
properties: {string: {type: 'string', enum: ['boolean']}},
1515
additionalProperties: false,
16+
patternProperties: {},
1617
required: ['string'],
1718
})
1819
})
1920

20-
test('translates `undefined` into `never`', () => {
21+
test('`undefined` -> `never`', () => {
2122
expect(translateXTypeToSchema('undefined')).toEqual({not: {}})
2223
})
2324

24-
test('handles OR', () => {
25+
test('OR', () => {
2526
expect(translateXTypeToSchema(['string', 'number'])).toEqual({
2627
anyOf: [{type: 'string'}, {type: 'number'}],
2728
})
@@ -49,13 +50,38 @@ describe('adapter', () => {
4950
Conditional: {type: 'string'},
5051
},
5152
additionalProperties: false,
53+
patternProperties: {},
5254
required: ['Required'],
5355
})
5456
})
5557

56-
test('handles literal $schema', () => {
58+
test('literal $schema', () => {
5759
expect(translateXTypeToSchema({$schema: {type: 'string'}})).toEqual({
5860
type: 'string',
5961
})
6062
})
63+
64+
test('string formats and modifiers', () => {
65+
expect(translateXTypeToSchema('string::date-time')).toEqual({
66+
type: 'string',
67+
format: 'date-time',
68+
})
69+
expect(
70+
translateXTypeToSchema({
71+
'string::pattern(some pattern)': 'string::min(10)::max(100)',
72+
})
73+
).toEqual({
74+
type: 'object',
75+
patternProperties: {
76+
'some pattern': {
77+
type: 'string',
78+
maxLength: 100,
79+
minLength: 10,
80+
},
81+
},
82+
properties: {},
83+
additionalProperties: false,
84+
required: [],
85+
})
86+
})
6187
})

applications/__tests__/e2e.test.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,17 @@ describe('bundle', () => {
107107

108108
test('openapi that contains literal $schema', () => {
109109
const {stdout} = runCommand(
110-
'redocly bundle applications/resources/openapi-literal-schema.yaml --config=applications/x-redocly.yaml'
110+
'redocly bundle applications/resources/openapi-literal-schema.yaml --config=applications/x-redocly.yaml'
111111
)
112112
expect(stdout).toMatchSnapshot()
113113
})
114+
115+
test('generate x-types from JSON Schemas', () => {
116+
const {stdout} = runCommand(
117+
'redocly bundle applications/resources/pets.yaml --config=applications/generate-x-types-redocly.yaml'
118+
)
119+
expect(stdout).toMatchFileSnapshot('pets-to-x-types.yaml')
120+
})
114121
})
115122

116123
describe('lint', () => {

0 commit comments

Comments
 (0)