Skip to content

Commit

Permalink
Disable ajv strict mode by default, but allow to use some strict checks
Browse files Browse the repository at this point in the history
  • Loading branch information
m-mohr committed Dec 11, 2021
1 parent 9dccd23 commit 5a15a2b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ Further options to add to the commands above:
- To validate against a specific local schema (e.g. an external extension): `--schemaMap https://stac-extensions.github.io/foobar/v1.0.0/schema.json=./json-schema/schema.json`
- To not verify SSL/TLS certificates: `--ignoreCerts`
- Add `--verbose` to get a more detailed output
- Add `--strict` to enable strict mode in validation for schemas and numbers (as defined by [ajv](https://ajv.js.org/strict-mode.html) for options `strictSchema`, `strictNumbers` and `strictTuples`)
- To lint local JSON files: `--lint` (add `--verbose` to get a diff with the changes required)
- To format / pretty-print local JSON files: `--format` (Attention: this will override the source files without warning!)

Expand Down Expand Up @@ -70,7 +71,8 @@ The schema map is an object instead of string separated with a `=` character.
"ignoreCerts": false,
"verbose": false,
"lint": true,
"format": false
"format": false,
"strict": true
}
```

Expand Down
8 changes: 7 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ let DEBUG = false;
let ajv = new Ajv({
formats: iriFormats,
allErrors: true,
strict: false,
logger: DEBUG ? console : false,
loadSchema: loadJsonFromUri
});
Expand All @@ -24,7 +25,7 @@ let verbose = false;
let schemaMap = {};
let schemaFolder = null;

let booleanArgs = ['verbose', 'ignoreCerts', 'lint', 'format', 'version'];
let booleanArgs = ['verbose', 'ignoreCerts', 'lint', 'format', 'version', 'strict'];

async function run(config) {
try {
Expand Down Expand Up @@ -100,6 +101,11 @@ async function run(config) {
if (config.ignoreCerts) {
process.env["NODE_TLS_REJECT_UNAUTHORIZED"] = 0;
}
if (config.strict) {
ajv.opts.strictSchema = true;
ajv.opts.strictNumbers = true;
ajv.opts.strictTuples = true;
}

if (typeof config.schemas === 'string') {
let stat = await fs.lstat(config.schemas);
Expand Down

0 comments on commit 5a15a2b

Please sign in to comment.