Skip to content

Commit

Permalink
Make rule/draft association less verbose (#183)
Browse files Browse the repository at this point in the history
Signed-off-by: Suprith KG <suprith7kg@gmail.com>
  • Loading branch information
Era-cell committed Mar 18, 2024
1 parent 1d924ae commit b1bb469
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 53 deletions.
64 changes: 25 additions & 39 deletions bindings/node/builtin.js
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
9 changes: 1 addition & 8 deletions bindings/node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,7 @@ async function transformer (root, path, ruleset, trails, originalSchema, from) {

module.exports = async (value, from, to) => {
let accumulator = _.cloneDeep(value)

if (!builtin.jsonschema[from]) {
throw new Error(`Invalid "from": ${from}`)
} else if (!builtin.jsonschema[from][to]) {
throw new Error(`Invalid "to": ${to}`)
}

for (const mapper of builtin.jsonschema[from][to]) {
for (const mapper of builtin.builtin(from, to)) {
const trails = walker(mapper.walker, accumulator, []).sort((a, b) => {
return b.path.length - a.path.length
})
Expand Down
9 changes: 5 additions & 4 deletions bindings/node/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const jsonschema = require('./jsonschema')
const alterschema = require('./index')
const packageJSON = require('../../package.json')
const METASCHEMAS = _.invert(require('../../metaschemas.json'))
const drafts = builtin.drafts

const JSON_SCHEMA_TEST_SUITE = path.resolve(__dirname, '..', '..', 'vendor', 'json-schema-test-suite')
const TESTS_BASE_DIRECTORY = path.resolve(JSON_SCHEMA_TEST_SUITE, 'tests')
Expand All @@ -24,8 +25,8 @@ const recursiveReadDirectory = (directory) => {
}, [])
}

for (const from of Object.keys(builtin.jsonschema)) {
for (const to of Object.keys(builtin.jsonschema[from])) {
for (const [draftIndex, from] of drafts.entries()) {
for (const to of drafts.slice(draftIndex + 1)) {
const tests = require(`../../test/rules/jsonschema-${from}-to-${to}.json`)
for (const testCase of tests) {
tap.test(`${from} => ${to}: ${testCase.name}`, async (test) => {
Expand Down Expand Up @@ -101,7 +102,7 @@ const BLACKLIST = [
'refRemote'
]

for (const from of Object.keys(builtin.jsonschema)) {
for (const [draftIndex, from] of drafts.entries()) {
// TODO: Support running draft3 tests
if (from === 'draft3') {
continue
Expand Down Expand Up @@ -141,7 +142,7 @@ for (const from of Object.keys(builtin.jsonschema)) {
}

const index = testCase.tests.indexOf(instance)
for (const to of Object.keys(builtin.jsonschema[from])) {
for (const to of drafts.slice(draftIndex + 1)) {
tap.test(`${suiteName} (${from} -> ${to}) ${testCase.description} #${index}`, async (test) => {
// We need at least an arbitrary id to make @hyperjump/json-schema work
const id = `https://alterschema.sourcemeta.com/${_.kebabCase(testCase.description)}/${index}`
Expand Down
4 changes: 2 additions & 2 deletions www/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ function onError (error) {
}

function refreshFrom (spec) {
setSpecificationOptions(from, Object.keys(builtin.jsonschema), spec)
setSpecificationOptions(from, builtin.drafts, spec)
from.dispatchEvent(new Event('change'))
}

Expand Down Expand Up @@ -145,7 +145,7 @@ transform.addEventListener('click', () => {

from.addEventListener('change', (event) => {
setSpecificationOptions(to,
Object.keys(builtin.jsonschema[event.target.value]).reverse())
builtin.drafts.slice(builtin.drafts.indexOf(event.target.value) + 1).reverse())
})

document.getElementById('version').innerText = `v${packageJSON.version}`
Expand Down

0 comments on commit b1bb469

Please sign in to comment.