@@ -3,13 +3,12 @@ const jsonSchema = require('./utils/json-schema');
3
3
4
4
/**
5
5
*
6
- * @param {'v2'|'v3'|'canary' } version - API's JSON schema version to check against
7
- * @param {string } name -JSON schema to retreive from "version" folder
6
+ * @param {string } name -JSON schema to retreive from "schemas" folder
8
7
*
9
8
* @returns {Object } - JSON schema file content
10
9
*/
11
- const getJSONDefinition = ( version , name ) => {
12
- const definitionPath = `./${ version } /${ name } ` ;
10
+ const getJSONDefinition = ( name ) => {
11
+ const definitionPath = `./schemas /${ name } ` ;
13
12
14
13
try {
15
14
return require ( definitionPath ) ;
@@ -21,45 +20,19 @@ const getJSONDefinition = (version, name) => {
21
20
} ;
22
21
23
22
/**
24
- * Resolves with a schema definition content. At the moment it contains unresolved `$ref` statements.
25
- * This method needs some additional work to be useful for outside consumer - contain resolved '$ref'.
26
- *
27
- * @param {Object } options
28
- * @param {'v2'|'v3'|'canary' } - API's JSON schema version to check against
29
- * @param {string } options.schema - name of JSON schema definition, comes from available optios returned by list()
30
- */
31
- const get = ( { version, schema} ) => {
32
- if ( ! list ( { version} ) . includes ( schema ) ) {
33
- return null ;
34
- }
35
-
36
- return getJSONDefinition ( version , schema ) ;
37
- } ;
38
-
39
- /**
40
- * Lists available JSON schema definitions for provided version
41
- *
42
- * @param {Object } options
43
- * @param {'v2'|'v3'|'v4'|'canary' } - API's JSON schema version to check against
23
+ * Lists available JSON schema definitions
44
24
*
45
25
* @returns {string[] } - list of available JSON schema definitions
46
26
*/
47
- const list = ( { version} ) => {
48
- if ( version === 'v2' ) {
49
- return require ( './v2' ) ;
50
- } else if ( version === 'v3' ) {
51
- return require ( './v3' ) ;
52
- } else if ( version === 'v4' || version === 'canary' ) {
53
- return require ( './canary' ) ;
54
- }
27
+ const list = ( ) => {
28
+ return require ( './schemas' ) ;
55
29
} ;
56
30
57
31
/**
58
32
* Validate method parameters
59
33
*
60
34
* @typedef {Object } ValidateOptions
61
35
* @property {Object } options.data - data to validate
62
- * @property {'v2'|'v3'|'canary' } options.version - API version to data belongs to, e.g.: 'v2', 'canary'
63
36
* @property {string } [options.schema] - name of the schema to validate against. Available schema names are returned by list() function
64
37
* @property {string } [options.definition] - name of the definition where schema belongs
65
38
*/
@@ -71,8 +44,8 @@ const list = ({version}) => {
71
44
*
72
45
* @returns {Promise } - resolves a promise if validation is successful and rejects with error details otherwise
73
46
*/
74
- const validate = ( { data, version , schema = '' , definition = schema . split ( '-' ) [ 0 ] } ) => {
75
- const schemaJSON = get ( { schema, version } ) ;
47
+ const validate = ( { data, schema, definition = schema ? .split ( '-' ) [ 0 ] } ) => {
48
+ const schemaJSON = getJSONDefinition ( schema ) ;
76
49
77
50
if ( ! schemaJSON ) {
78
51
throw new errors . IncorrectUsageError ( {
@@ -81,66 +54,13 @@ const validate = ({data, version, schema = '', definition = schema.split('-')[0]
81
54
} ) ;
82
55
}
83
56
84
- const definitionJSON = getJSONDefinition ( version , definition ) ;
57
+ const definitionJSON = getJSONDefinition ( definition ) ;
85
58
86
59
return jsonSchema . validate ( schemaJSON , definitionJSON , data ) ;
87
60
} ;
88
61
89
- /**
90
- * Resolves version into "internal" one.
91
- * 'canary' is used to identify latest available version. Currently it's 'v3' and once there's
92
- * next version introduced - 'v4' should be resolved to 'canary', and 'v3' should be returned as is.
93
- *
94
- * @param {'v2'|'v3'|'v4'|'canary' } - version to resolve into internaly available version
95
- *
96
- * @returns {'v2'|'v3'|'canary' } - resolved version
97
- */
98
- const resolveVersion = ( version ) => {
99
- switch ( version ) {
100
- case 'canary' :
101
- case 'v4' :
102
- return 'canary' ;
103
- case 'v3' :
104
- return 'v3' ;
105
- case 'v2' :
106
- return 'v2' ;
107
- default :
108
- throw new errors . IncorrectUsageError ( {
109
- message : `Unrecognised version ${ version } .`
110
- } ) ;
111
- }
112
- } ;
113
-
114
- /**
115
- * Versioned version of 'get' method, which returns a JSON schema definition if found
116
- *
117
- * @param {string } schema - name of JSON schema definitions
118
- * @param {string } version - API's JSON schema version to check against
119
- *
120
- * @returns {Object|null } JSON schema definition or null if it's not found
121
- */
122
- const versionedGet = ( schema , version = 'v4' ) => get ( { schema, version : resolveVersion ( version ) } ) ;
123
-
124
- /**
125
- * Versioned version of 'list' method, which lisists available JSON schema definitions for provided version
126
- *
127
- * @param {string } version - API's JSON schema version to check against
128
- *
129
- * @returns {string[] } - list of available JSON schema definitions
130
- */
131
- const versionedList = ( version = 'v4' ) => list ( { version : resolveVersion ( version ) } ) ;
132
-
133
- /**
134
- * Versioned version of 'validate' method, which validates objects against predefined JSON Schema
135
- *
136
- * @param {ValidateOptions } options
137
- *
138
- * @returns {Promise } - resolves a promise if validation is successful and rejects with error details otherwise
139
- */
140
- const versionedValidate = options => validate ( Object . assign ( options , { version : resolveVersion ( options . version || 'v4' ) } ) ) ;
141
-
142
62
module . exports = {
143
- get : versionedGet ,
144
- list : versionedList ,
145
- validate : versionedValidate
63
+ get : getJSONDefinition ,
64
+ list : list ,
65
+ validate : validate
146
66
} ;
0 commit comments