-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make rule/draft association less verbose (#183)
Signed-off-by: Suprith KG <suprith7kg@gmail.com>
- Loading branch information
Showing
4 changed files
with
33 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,28 @@ | ||
const DRAFT3_TO_DRAFT3 = require('../../rules/jsonschema-draft3-to-draft3.json') | ||
const DRAFT3_TO_DRAFT4 = require('../../rules/jsonschema-draft3-to-draft4.json') | ||
const DRAFT4_TO_DRAFT4 = require('../../rules/jsonschema-draft4-to-draft4.json') | ||
const DRAFT4_TO_DRAFT6 = require('../../rules/jsonschema-draft4-to-draft6.json') | ||
const DRAFT6_TO_DRAFT7 = require('../../rules/jsonschema-draft6-to-draft7.json') | ||
const DRAFT6_TO_DRAFT6 = require('../../rules/jsonschema-draft6-to-draft6.json') | ||
const DRAFT7_TO_V2019_09 = require('../../rules/jsonschema-draft7-to-2019-09.json') | ||
const DRAFT7_TO_DRAFT7 = require('../../rules/jsonschema-draft7-to-draft7.json') | ||
const V2019_09_TO_V2020_12 = require('../../rules/jsonschema-2019-09-to-2020-12.json') | ||
const V2019_09_TO_V2019_09 = require('../../rules/jsonschema-2019-09-to-2019-09.json') | ||
const V2020_12_TO_V2020_12 = require('../../rules/jsonschema-2020-12-to-2020-12.json') | ||
// TODO: Find a way to specify transitiveness in a less verbose manner: DONE | ||
|
||
// TODO: Find a way to specify transitiveness in a less verbose manner | ||
module.exports = { | ||
jsonschema: { | ||
draft3: { | ||
draft4: [DRAFT3_TO_DRAFT3, DRAFT3_TO_DRAFT4, DRAFT4_TO_DRAFT4], | ||
draft6: [DRAFT3_TO_DRAFT3, DRAFT3_TO_DRAFT4, DRAFT4_TO_DRAFT4, DRAFT4_TO_DRAFT6, DRAFT6_TO_DRAFT6], | ||
draft7: [DRAFT3_TO_DRAFT3, DRAFT3_TO_DRAFT4, DRAFT4_TO_DRAFT4, DRAFT4_TO_DRAFT6, DRAFT6_TO_DRAFT6, DRAFT6_TO_DRAFT7, DRAFT7_TO_DRAFT7], | ||
'2019-09': [DRAFT3_TO_DRAFT3, DRAFT3_TO_DRAFT4, DRAFT4_TO_DRAFT4, DRAFT4_TO_DRAFT6, DRAFT6_TO_DRAFT6, DRAFT6_TO_DRAFT7, DRAFT7_TO_DRAFT7, DRAFT7_TO_V2019_09, V2019_09_TO_V2019_09], | ||
'2020-12': [DRAFT3_TO_DRAFT3, DRAFT3_TO_DRAFT4, DRAFT4_TO_DRAFT4, DRAFT4_TO_DRAFT6, DRAFT6_TO_DRAFT6, DRAFT6_TO_DRAFT7, DRAFT7_TO_DRAFT7, DRAFT7_TO_V2019_09, V2019_09_TO_V2019_09, V2019_09_TO_V2020_12, V2020_12_TO_V2020_12] | ||
}, | ||
draft4: { | ||
draft6: [DRAFT4_TO_DRAFT4, DRAFT4_TO_DRAFT6, DRAFT6_TO_DRAFT6], | ||
draft7: [DRAFT4_TO_DRAFT4, DRAFT4_TO_DRAFT6, DRAFT6_TO_DRAFT6, DRAFT6_TO_DRAFT7, DRAFT7_TO_DRAFT7], | ||
'2019-09': [DRAFT4_TO_DRAFT4, DRAFT4_TO_DRAFT6, DRAFT6_TO_DRAFT6, DRAFT6_TO_DRAFT7, DRAFT7_TO_DRAFT7, DRAFT7_TO_V2019_09, V2019_09_TO_V2019_09], | ||
'2020-12': [DRAFT4_TO_DRAFT4, DRAFT4_TO_DRAFT6, DRAFT6_TO_DRAFT6, DRAFT6_TO_DRAFT7, DRAFT7_TO_DRAFT7, DRAFT7_TO_V2019_09, V2019_09_TO_V2019_09, V2019_09_TO_V2020_12, V2020_12_TO_V2020_12] | ||
}, | ||
draft6: { | ||
draft7: [DRAFT6_TO_DRAFT6, DRAFT6_TO_DRAFT7, DRAFT7_TO_DRAFT7], | ||
'2019-09': [DRAFT6_TO_DRAFT6, DRAFT6_TO_DRAFT7, DRAFT7_TO_DRAFT7, DRAFT7_TO_V2019_09, V2019_09_TO_V2019_09], | ||
'2020-12': [DRAFT6_TO_DRAFT6, DRAFT6_TO_DRAFT7, DRAFT7_TO_DRAFT7, DRAFT7_TO_V2019_09, V2019_09_TO_V2019_09, V2019_09_TO_V2020_12, V2020_12_TO_V2020_12] | ||
}, | ||
draft7: { | ||
'2019-09': [DRAFT7_TO_DRAFT7, DRAFT7_TO_V2019_09, V2019_09_TO_V2019_09], | ||
'2020-12': [DRAFT7_TO_DRAFT7, DRAFT7_TO_V2019_09, V2019_09_TO_V2019_09, V2019_09_TO_V2020_12, V2020_12_TO_V2020_12] | ||
}, | ||
'2019-09': { | ||
'2020-12': [V2019_09_TO_V2019_09, V2019_09_TO_V2020_12, V2020_12_TO_V2020_12] | ||
} | ||
// mappers to store all the mappers of all drafts from draft3 to draft7 | ||
const drafts = ['draft3', 'draft4', 'draft6', 'draft7', '2019-09', '2020-12'] | ||
const mappers = drafts.flatMap((draft, index) => { | ||
const rules = [require(`../../rules/jsonschema-${draft}-to-${draft}.json`)] | ||
if (index + 1 < drafts.length) { | ||
rules.push(require(`../../rules/jsonschema-${draft}-to-${drafts[index + 1]}.json`)) | ||
} | ||
return rules | ||
}) | ||
|
||
// indexMapper maps drafts to their index in the mappers array. This is used to find the subarray of mappers to be returned. | ||
const indexMapper = new Map(drafts.map((draft, index) => [draft, index * 2])) | ||
|
||
exports.builtin = (from, to) => { | ||
if (!indexMapper.has(from)) { | ||
throw new Error(`Invalid "from": ${from}`) | ||
} else if (!indexMapper.has(to)) { | ||
throw new Error(`Invalid "to": ${to}`) | ||
} | ||
|
||
const fromIndex = indexMapper.get(from) | ||
const toIndex = indexMapper.get(to) | ||
return mappers.slice(fromIndex, toIndex + 1) | ||
} | ||
|
||
exports.drafts = drafts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters