@@ -19,14 +19,48 @@ const unsupportedKeys = [
1919 'properties' , 'items' , 'required' , 'nullable' , 'title' , 'description' ,
2020 'default' , 'oneOf' , 'allOf' , 'anyOf' , 'not' , 'additionalProperties' ,
2121 'format' , 'discriminator' , 'readOnly' , 'writeOnly' , 'xml' , 'externalDocs' ,
22- 'deprecated' , 'example' ,
22+ 'deprecated' ,
2323] ;
2424const isUnsupportedKey = R . anyPass ( R . map ( hasKey , unsupportedKeys ) ) ;
2525
26- // purposely in the order defined in the JSON Schema spec, integer is an OAS 3 specific addition and thus is at the end
2726const types = [ 'boolean' , 'object' , 'array' , 'number' , 'string' , 'integer' ] ;
2827const isValidType = R . anyPass ( R . map ( hasValue , types ) ) ;
2928
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+
3064/**
3165 * Parse Parameter Object's Schema Object
3266 *
@@ -53,6 +87,7 @@ function parseSchemaObject(context) {
5387
5488 const parseMember = R . cond ( [
5589 [ hasKey ( 'type' ) , parseType ] ,
90+ [ hasKey ( 'example' ) , e => e . clone ( ) ] ,
5691
5792 [ isUnsupportedKey , createUnsupportedMemberWarning ( namespace , name ) ] ,
5893 [ isExtension , ( ) => new namespace . elements . ParseResult ( ) ] ,
@@ -61,6 +96,7 @@ function parseSchemaObject(context) {
6196
6297 return pipeParseResult ( namespace ,
6398 parseObject ( context , name , parseMember , [ ] ) ,
99+ R . curry ( validateValuesMatchSchema ) ( context ) ,
64100 ( schema ) => {
65101 const type = schema . getValue ( 'type' ) ;
66102 let element ;
@@ -76,7 +112,12 @@ function parseSchemaObject(context) {
76112 } else if ( type === 'boolean' ) {
77113 element = new namespace . elements . Boolean ( ) ;
78114 } 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 ] ) ;
80121 }
81122
82123 return element ;
0 commit comments