@@ -7,39 +7,6 @@ const url = require('url');
7
7
const yaml = require ( 'yaml' ) ;
8
8
const should = require ( 'should/as-function' ) ;
9
9
const maybe = require ( 'call-me-maybe' ) ;
10
- let ajv = require ( 'ajv' ) ( {
11
- $data : true ,
12
- allErrors : true ,
13
- verbose : true ,
14
- jsonPointers : true ,
15
- patternGroups : true ,
16
- extendRefs : true // optional, current default is to 'fail', spec behaviour is to 'ignore'
17
- } ) ;
18
- //meta: false, // optional, to prevent adding draft-06 meta-schema
19
-
20
- let ajvFormats = require ( 'ajv/lib/compile/formats.js' ) ;
21
- ajv . addFormat ( 'uriref' , ajvFormats . full [ 'uri-reference' ] ) ;
22
- ajv . addMetaSchema ( require ( 'ajv/lib/refs/json-schema-draft-04.json' ) ) ;
23
- ajv . _refs [ 'http://json-schema.org/schema' ] = 'http://json-schema.org/draft-04/schema' ; // optional, using unversioned URI is out of spec
24
- let metaSchema = require ( 'ajv/lib/refs/json-schema-v5.json' ) ;
25
- ajv . addMetaSchema ( metaSchema ) ;
26
- ajv . _opts . defaultMeta = metaSchema . id ;
27
-
28
- const bae = require ( 'better-ajv-errors' ) ;
29
-
30
- class JSONSchemaError extends Error {
31
- constructor ( message ) {
32
- super ( message ) ;
33
- this . name = 'JSONSchemaError' ;
34
- }
35
- }
36
-
37
- class CLIError extends Error {
38
- constructor ( message ) {
39
- super ( message ) ;
40
- this . name = 'CLIError' ;
41
- }
42
- }
43
10
44
11
const common = require ( 'oas-kit-common' ) ;
45
12
const jptr = require ( 'reftools/lib/jptr.js' ) ;
@@ -51,11 +18,6 @@ const sw = require('oas-schema-walker');
51
18
const linter = require ( 'oas-linter' ) ;
52
19
const resolver = require ( 'oas-resolver' ) ;
53
20
54
- const jsonSchema = require ( './schemas/json_v5.json' ) ;
55
- const validateMetaSchema = ajv . compile ( jsonSchema ) ;
56
- let openapi3Schema = require ( './schemas/openapi-3.0.json' ) ;
57
- let validateOpenAPI3 = ajv . compile ( openapi3Schema ) ;
58
-
59
21
const dummySchema = { anyOf : { } } ;
60
22
const emptySchema = { } ;
61
23
let refSeen = { } ;
@@ -95,20 +57,6 @@ function validateHeaderName(name) {
95
57
return / ^ [ A - Z a - z 0 - 9 ! # \- \$ % & ' \* \+ \\ \. \^ _ ` \| ~ ] + $ / . test ( name ) ;
96
58
}
97
59
98
- function validateSchema ( schema , openapi , options ) {
99
- validateMetaSchema ( schema ) ;
100
- let errors = validateSchema . errors ;
101
- if ( errors && errors . length ) {
102
- if ( options . prettify ) {
103
- const errorStr = bae ( schema , openapi , errors ) ;
104
- throw ( new CLIError ( errorStr ) ) ;
105
- }
106
- throw ( new JSONSchemaError ( 'Schema invalid:\n' + yaml . stringify ( errors ) ) ) ;
107
- }
108
- options . schema = schema ;
109
- return ! ( errors && errors . length ) ;
110
- }
111
-
112
60
function checkSubSchema ( schema , parent , state ) {
113
61
let prop = state . property ;
114
62
if ( prop ) contextAppend ( state . options , prop ) ;
@@ -334,7 +282,6 @@ function checkSubSchema(schema, parent, state) {
334
282
if ( state . options . lint ) state . options . linter ( 'externalDocs' , schema . externalDocs , 'externalDocs' , state . options ) ;
335
283
}
336
284
if ( prop ) state . options . context . pop ( ) ;
337
- if ( ! prop || prop === 'schema' ) validateSchema ( schema , state . openapi , state . options ) ; // top level only
338
285
}
339
286
340
287
function checkSchema ( schema , parent , prop , openapi , options ) {
@@ -497,7 +444,7 @@ function checkLink(link, openapi, options) {
497
444
if ( typeof link . $ref !== 'undefined' ) {
498
445
let ref = link . $ref ;
499
446
should ( ref ) . be . type ( 'string' ) ;
500
- if ( refSeen [ ref ] ) return true ;
447
+ if ( refSeen [ ref ] ) return true ; // bail out
501
448
refSeen [ ref ] = true ;
502
449
if ( options . lint ) options . linter ( 'reference' , link , '$ref' , options ) ;
503
450
link = resolveInternal ( openapi , ref ) ;
@@ -536,7 +483,7 @@ function checkHeader(header, contextServers, openapi, options) {
536
483
if ( typeof header . $ref !== 'undefined' ) {
537
484
let ref = header . $ref ;
538
485
should ( ref ) . be . type ( 'string' ) ;
539
- if ( refSeen [ ref ] ) return true ;
486
+ if ( refSeen [ ref ] ) return true ; // bail out
540
487
refSeen [ ref ] = true ;
541
488
if ( options . lint ) options . linter ( 'reference' , header , '$ref' , options ) ;
542
489
header = resolveInternal ( openapi , ref ) ;
@@ -582,7 +529,7 @@ function checkResponse(response, key, contextServers, openapi, options) {
582
529
if ( typeof response . $ref !== 'undefined' ) {
583
530
let ref = response . $ref ;
584
531
should ( ref ) . be . type ( 'string' ) ;
585
- if ( refSeen [ ref ] ) return true ;
532
+ if ( refSeen [ ref ] ) return true ; // bail out
586
533
refSeen [ ref ] = true ;
587
534
if ( options . lint ) options . linter ( 'reference' , response , '$ref' , options ) ;
588
535
response = resolveInternal ( openapi , ref ) ;
@@ -620,13 +567,16 @@ function checkResponse(response, key, contextServers, openapi, options) {
620
567
}
621
568
622
569
function checkParam ( param , index , path , contextServers , openapi , options ) {
623
- contextAppend ( options , index ) ;
624
570
const ref = param . $ref ;
571
+ contextAppend ( options , index ) ;
625
572
if ( typeof param . $ref !== 'undefined' ) {
626
573
should ( ref ) . be . type ( 'string' ) ;
627
- if ( refSeen [ ref ] ) return refSeen [ ref ] ;
628
574
if ( options . lint ) options . linter ( 'reference' , param , '$ref' , options ) ;
629
575
param = resolveInternal ( openapi , ref ) ;
576
+ if ( refSeen [ ref ] && ( param . in !== 'path' ) ) {
577
+ options . context . pop ( ) ;
578
+ return param ; // bail out
579
+ }
630
580
should ( param ) . not . be . exactly ( false , 'Cannot resolve reference: ' + ref ) ;
631
581
}
632
582
should ( param ) . have . property ( 'name' ) ;
@@ -954,16 +904,6 @@ function validateInner(openapi, options, callback) {
954
904
setupOptions ( options , openapi ) ;
955
905
let contextServers = [ ] ;
956
906
957
- if ( options . jsonschema ) {
958
- let schemaStr = fs . readFileSync ( options . jsonschema , 'utf8' ) ;
959
- openapi3Schema = yaml . parse ( schemaStr , { schema :'core' } ) ;
960
- validateOpenAPI3 = ajv . compile ( openapi3Schema ) ;
961
- }
962
-
963
- if ( options . validateSchema === 'first' ) {
964
- schemaValidate ( openapi , options ) ;
965
- }
966
-
967
907
should ( openapi ) . be . an . Object ( ) ;
968
908
should ( openapi ) . not . have . key ( 'swagger' ) ;
969
909
should ( openapi ) . have . key ( 'openapi' ) ;
@@ -1383,10 +1323,6 @@ function validateInner(openapi, options, callback) {
1383
1323
options . context . pop ( ) ;
1384
1324
}
1385
1325
1386
- if ( ! options . validateSchema || ( options . validateSchema === 'last' ) ) {
1387
- schemaValidate ( openapi , options ) ;
1388
- }
1389
-
1390
1326
options . valid = ! options . expectFailure ;
1391
1327
if ( options . lint ) options . linter ( 'openapi' , openapi , '' , options ) ;
1392
1328
@@ -1418,18 +1354,6 @@ function validateInner(openapi, options, callback) {
1418
1354
} ) ) ;
1419
1355
}
1420
1356
1421
- function schemaValidate ( openapi , options ) {
1422
- validateOpenAPI3 ( openapi ) ;
1423
- let errors = validateOpenAPI3 . errors ;
1424
- if ( errors && errors . length ) {
1425
- if ( options . prettify ) {
1426
- const errorStr = bae ( options . schema , openapi , errors , { indent : 2 } ) ;
1427
- throw ( new CLIError ( errorStr ) ) ;
1428
- }
1429
- throw ( new JSONSchemaError ( 'Failed OpenAPI3 schema validation: ' + JSON . stringify ( errors , null , 2 ) ) ) ;
1430
- }
1431
- }
1432
-
1433
1357
function setupOptions ( options , openapi ) {
1434
1358
refSeen = { } ;
1435
1359
options . valid = false ;
@@ -1446,10 +1370,8 @@ function setupOptions(options,openapi) {
1446
1370
options . linterResults = linter . getResults ;
1447
1371
}
1448
1372
if ( ! options . cache ) options . cache = { } ;
1449
- options . schema = openapi3Schema ;
1450
1373
options . metadata = { lines : - 1 , count : { } } ;
1451
1374
if ( ( options . text ) && ( typeof options . text === 'string' ) ) options . metadata . lines = options . text . split ( '\n' ) . length ;
1452
- options . ajv = ajv ;
1453
1375
}
1454
1376
1455
1377
function validate ( openapi , options , callback ) {
@@ -1494,7 +1416,5 @@ module.exports = {
1494
1416
validateInner : validateInner ,
1495
1417
validate : validate ,
1496
1418
microValidate : microValidate ,
1497
- optionallyValidate : optionallyValidate ,
1498
- JSONSchemaError : JSONSchemaError ,
1499
- CLIError : CLIError
1419
+ optionallyValidate : optionallyValidate
1500
1420
}
0 commit comments