@@ -19,14 +19,48 @@ const unsupportedKeys = [
19
19
'properties' , 'items' , 'required' , 'nullable' , 'title' , 'description' ,
20
20
'default' , 'oneOf' , 'allOf' , 'anyOf' , 'not' , 'additionalProperties' ,
21
21
'format' , 'discriminator' , 'readOnly' , 'writeOnly' , 'xml' , 'externalDocs' ,
22
- 'deprecated' , 'example' ,
22
+ 'deprecated' ,
23
23
] ;
24
24
const isUnsupportedKey = R . anyPass ( R . map ( hasKey , unsupportedKeys ) ) ;
25
25
26
- // purposely in the order defined in the JSON Schema spec, integer is an OAS 3 specific addition and thus is at the end
27
26
const types = [ 'boolean' , 'object' , 'array' , 'number' , 'string' , 'integer' ] ;
28
27
const isValidType = R . anyPass ( R . map ( hasValue , types ) ) ;
29
28
29
+ const typeToElementNameMap = {
30
+ array : 'array' ,
31
+ boolean : 'boolean' ,
32
+ integer : 'number' ,
33
+ null : 'null' ,
34
+ number : 'number' ,
35
+ object : 'object' ,
36
+ string : 'string' ,
37
+ } ;
38
+
39
+ // Returns whether the given element value matches the provided schema type
40
+ const valueMatchesType = ( type , value ) => {
41
+ const expectedElementType = typeToElementNameMap [ type ] ;
42
+ return value . element === expectedElementType ;
43
+ } ;
44
+
45
+ function validateValuesMatchSchema ( context , schema ) {
46
+ const validate = ( member ) => {
47
+ const type = schema . getValue ( 'type' ) ;
48
+ if ( type && ! valueMatchesType ( type , member . value ) ) {
49
+ return createWarning ( context . namespace ,
50
+ `'${ name } ' '${ member . key . toValue ( ) } ' does not match expected type '${ type } '` , member . value ) ;
51
+ }
52
+
53
+ return member ;
54
+ } ;
55
+
56
+ const parseMember = R . cond ( [
57
+ [ hasKey ( 'example' ) , validate ] ,
58
+ [ R . T , e => e ] ,
59
+ ] ) ;
60
+
61
+ return parseObject ( context , name , parseMember ) ( schema ) ;
62
+ }
63
+
30
64
/**
31
65
* Parse Parameter Object's Schema Object
32
66
*
@@ -53,6 +87,7 @@ function parseSchemaObject(context) {
53
87
54
88
const parseMember = R . cond ( [
55
89
[ hasKey ( 'type' ) , parseType ] ,
90
+ [ hasKey ( 'example' ) , e => e . clone ( ) ] ,
56
91
57
92
[ isUnsupportedKey , createUnsupportedMemberWarning ( namespace , name ) ] ,
58
93
[ isExtension , ( ) => new namespace . elements . ParseResult ( ) ] ,
@@ -61,6 +96,7 @@ function parseSchemaObject(context) {
61
96
62
97
return pipeParseResult ( namespace ,
63
98
parseObject ( context , name , parseMember , [ ] ) ,
99
+ R . curry ( validateValuesMatchSchema ) ( context ) ,
64
100
( schema ) => {
65
101
const type = schema . getValue ( 'type' ) ;
66
102
let element ;
@@ -76,7 +112,12 @@ function parseSchemaObject(context) {
76
112
} else if ( type === 'boolean' ) {
77
113
element = new namespace . elements . Boolean ( ) ;
78
114
} else {
79
- element = new namespace . elements . ParseResult ( [ ] ) ;
115
+ return new namespace . elements . ParseResult ( [ ] ) ;
116
+ }
117
+
118
+ const example = schema . get ( 'example' ) ;
119
+ if ( example ) {
120
+ element . attributes . set ( 'samples' , [ example ] ) ;
80
121
}
81
122
82
123
return element ;
0 commit comments