Skip to content

Commit ba5ee03

Browse files
committed
Removed notion of "version" in admin-api-schema's API
refs https://github.com/TryGhost/Toolbox/issues/314 - The Ghost API has gotten rid of the "version", so there's no need to keep this concept in the JSON Schema's either as it would always be called with a single "latest" or "current" state
1 parent a085239 commit ba5ee03

File tree

3 files changed

+65
-334
lines changed

3 files changed

+65
-334
lines changed

packages/admin-api-schema/README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,7 @@ When used from Ghost core in validation layer:
6565
const jsonSchema = require('@tryghost/admin-api-schema');
6666
const validate = async (apiConfig, frame) => await jsonSchema.validate({
6767
data: frame.data,
68-
schema: `${apiConfig.docName}-${apiConfig.method}`,
69-
version: 'canary'
68+
schema: `${apiConfig.docName}-${apiConfig.method}`
7069
});
7170
```
7271

packages/admin-api-schema/lib/admin-api-schema.js

Lines changed: 12 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@ const jsonSchema = require('./utils/json-schema');
33

44
/**
55
*
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
87
*
98
* @returns {Object} - JSON schema file content
109
*/
11-
const getJSONDefinition = (version, name) => {
12-
const definitionPath = `./${version}/${name}`;
10+
const getJSONDefinition = (name) => {
11+
const definitionPath = `./schemas/${name}`;
1312

1413
try {
1514
return require(definitionPath);
@@ -21,45 +20,19 @@ const getJSONDefinition = (version, name) => {
2120
};
2221

2322
/**
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
4424
*
4525
* @returns {string[]} - list of available JSON schema definitions
4626
*/
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');
5529
};
5630

5731
/**
5832
* Validate method parameters
5933
*
6034
* @typedef {Object} ValidateOptions
6135
* @property {Object} options.data - data to validate
62-
* @property {'v2'|'v3'|'canary'} options.version - API version to data belongs to, e.g.: 'v2', 'canary'
6336
* @property {string} [options.schema] - name of the schema to validate against. Available schema names are returned by list() function
6437
* @property {string} [options.definition] - name of the definition where schema belongs
6538
*/
@@ -71,8 +44,8 @@ const list = ({version}) => {
7144
*
7245
* @returns {Promise} - resolves a promise if validation is successful and rejects with error details otherwise
7346
*/
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);
7649

7750
if (!schemaJSON) {
7851
throw new errors.IncorrectUsageError({
@@ -81,66 +54,13 @@ const validate = ({data, version, schema = '', definition = schema.split('-')[0]
8154
});
8255
}
8356

84-
const definitionJSON = getJSONDefinition(version, definition);
57+
const definitionJSON = getJSONDefinition(definition);
8558

8659
return jsonSchema.validate(schemaJSON, definitionJSON, data);
8760
};
8861

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-
14262
module.exports = {
143-
get: versionedGet,
144-
list: versionedList,
145-
validate: versionedValidate
63+
get: getJSONDefinition,
64+
list: list,
65+
validate: validate
14666
};

0 commit comments

Comments
 (0)