Skip to content

Commit

Permalink
Add CLI command to validate one or all schemas against the strict met…
Browse files Browse the repository at this point in the history
…a schema (#4035)
  • Loading branch information
Vampire authored Aug 30, 2024
1 parent c90be9e commit 38e813b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ There is an [unofficial draft-07][draft-07-unofficial-strict] schema that uses J
- `type` can't be an array, which is intentional, `anyOf`/`oneOf` should be used in this case
- It links to [understanding-json-schema](https://json-schema.org/understanding-json-schema/index.html) for each hint/check

To check your schema against that schema, use `npm run check-strict -- --SchemaName=<schemaName.json>`.

**Don't forget** add test files.

- Always be consistent across your schema: order properties and describe in the same style.
Expand Down
35 changes: 29 additions & 6 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ const SchemaDialects = [
{ draftVersion: 'draft-03', url: 'http://json-schema.org/draft-03/schema#', isActive: false, isTooHigh: false },
]

/** @type {{ _: string[], help?: boolean, lint?: boolean, SchemaName?: string, 'unstable-check-with'?: string }} */
/** @type {{ _: string[], help?: boolean, SchemaName?: string, ExplicitTestFile?: string, 'unstable-check-with'?: string }} */
const argv = /** @type {any} */ (
minimist(process.argv.slice(2), {
string: ['SchemaName', 'unstable-check-with'],
Expand Down Expand Up @@ -208,17 +208,31 @@ async function forEachFile(/** @type {ForEachTestFile} */ obj) {
const data = await obj?.onSchemaFile?.(schema)

if (obj?.onPositiveTestFile) {
const positiveTestDir = path.join(TestPositiveDir, schemaId)
if (await exists(positiveTestDir)) {
for (const testfile of await fs.readdir(positiveTestDir)) {
const testfilePath = path.join(TestPositiveDir, schemaId, testfile)
if (argv.ExplicitTestFile) {
if (argv.ExplicitTestFile === '<all>') {
for (const testfile of await fs.readdir(SchemaDir)) {
const testfilePath = path.join(SchemaDir, testfile)
let file = await toTestFile(testfilePath)
await obj.onPositiveTestFile(schema, file, data)
}
} else {
const testfilePath = path.join(SchemaDir, argv.ExplicitTestFile)
let file = await toTestFile(testfilePath)
await obj.onPositiveTestFile(schema, file, data)
}
} else {
const positiveTestDir = path.join(TestPositiveDir, schemaId)
if (await exists(positiveTestDir)) {
for (const testfile of await fs.readdir(positiveTestDir)) {
const testfilePath = path.join(TestPositiveDir, schemaId, testfile)
let file = await toTestFile(testfilePath)
await obj.onPositiveTestFile(schema, file, data)
}
}
}
}

if (obj?.onNegativeTestFile) {
if (!argv.ExplicitTestFile && obj?.onNegativeTestFile) {
const negativeTestDir = path.join(TestNegativeDir, schemaId)
if (await exists(negativeTestDir)) {
for (const testfile of await fs.readdir(negativeTestDir)) {
Expand Down Expand Up @@ -697,6 +711,12 @@ async function taskCheck() {
await printCountSchemaVersions()
}

async function taskCheckStrict() {
argv.ExplicitTestFile = argv.SchemaName || '<all>'
argv.SchemaName = 'metaschema-draft-07-unofficial-strict.json'
await taskCheck()
}

async function taskCheckRemote() {
console.info('TODO')
}
Expand Down Expand Up @@ -1455,13 +1475,15 @@ TASKS:
new-schema: Create a new JSON schema
lint: Run less-important checks on schemas
check: Run all build checks
check-strict: Checks all or the given schema against the strict meta schema
check-remote: Run all build checks for remote schemas
coverage: Generate code coverage for a schema
maintenance: Run maintenance checks
EXAMPLES:
node ./cli.js check
node ./cli.js check --SchemaName=schema-catalog.json
node ./cli.js check-strict --SchemaName=schema-catalog.json
`

if (!argv._[0]) {
Expand All @@ -1484,6 +1506,7 @@ EXAMPLES:
'new-schema': taskNewSchema,
lint: taskLint,
check: taskCheck,
'check-strict': taskCheckStrict,
'check-remote': taskCheckRemote,
report: taskReport,
maintenance: taskMaintenance,
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"eslint:fix": "eslint --fix ./cli.js",
"new-schema": "node --no-deprecation ./cli.js new-schema",
"check": "node ./cli.js check",
"check-strict": "node ./cli.js check-strict",
"check-remote": "node ./cli.js check-remote",
"report": "node ./cli.js report",
"maintenance": "node ./cli.js maintenance",
Expand Down

0 comments on commit 38e813b

Please sign in to comment.