Skip to content

Commit

Permalink
Merge a7604a6 into 58d6f07
Browse files Browse the repository at this point in the history
  • Loading branch information
agaskill committed Jul 6, 2022
2 parents 58d6f07 + a7604a6 commit 6f0fe51
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 6 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,14 @@ These modules can be written in TypeScript if you have `ts-node` installed.

For example, you can use `-c ajv-keywords` to add all keywords from [ajv-keywords](https://github.com/ajv-validator/ajv-keywords) package or `-c ajv-keywords/keywords/typeof` to add only typeof keyword.

##### `-x` - exclude data files

If the files matched by the `-d` glob pattern(s) are too broad, an ignore pattern can be added using `-x`. This pattern is passed directly to the [glob library](https://github.com/isaacs/node-glob#options) as the `ignore` option. Only a single ignore pattern can be specified, but it can be quite flexible:

```sh
ajv -s test/schema.json -d test/*.json -x "test/+(schema|legacy-*).json"
```

#### Options

- `--errors=`: error reporting format. Possible values:
Expand Down
2 changes: 1 addition & 1 deletion src/commands/ajv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export default function (argv: ParsedArgs): AjvCore {

try {
registerer = require("ts-node").register()
} catch (err) {
} catch (err: any) {
/* istanbul ignore next */
if (err.code === "MODULE_NOT_FOUND") {
throw new Error(
Expand Down
2 changes: 2 additions & 0 deletions src/commands/help.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,11 @@ parameters
-r referenced schema(s)
-m meta schema(s)
-c custom keywords/formats definitions
-x data file pattern to be ignored
-d, -r, -m, -c can be globs and can be used multiple times
glob should be enclosed in double quotes
-x can only be used once, but can be a glob
-c module(s) should export a function that accepts Ajv instance as parameter
(file path should start with ".", otherwise used as require package)
.json extension can be omitted (but should be used in globs)`)
Expand Down
8 changes: 4 additions & 4 deletions src/commands/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ import * as yaml from "js-yaml"
import * as JSON5 from "json5"
import {AnyValidateFunction} from "ajv/dist/core"

export function getFiles(args: string | string[]): string[] {
export function getFiles(args: string | string[], ignore?: string): string[] {
let files: string[] = []
if (Array.isArray(args)) args.forEach(_getFiles)
else _getFiles(args)
return files

function _getFiles(fileOrPattern: string): void {
if (glob.hasMagic(fileOrPattern)) {
const dataFiles = glob.sync(fileOrPattern, {cwd: process.cwd()})
const dataFiles = glob.sync(fileOrPattern, {cwd: process.cwd(), ignore})
files = files.concat(dataFiles)
} else {
files.push(fileOrPattern)
Expand Down Expand Up @@ -51,7 +51,7 @@ export function openFile(filename: string, suffix: string): any {
} catch (e) {
json = require(file)
}
} catch (err) {
} catch (err: any) {
const msg: string = err.message
console.error(`error: ${msg.replace(" module", " " + suffix)}`)
process.exit(2)
Expand Down Expand Up @@ -80,7 +80,7 @@ export function compile(ajv: Ajv, schemaFile: string): AnyValidateFunction {
const schema = openFile(schemaFile, "schema")
try {
return ajv.compile(schema)
} catch (err) {
} catch (err: any) {
console.error(`schema ${schemaFile} is invalid`)
console.error(`error: ${err.message}`)
process.exit(1)
Expand Down
5 changes: 4 additions & 1 deletion src/commands/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ const cmd: Command = {
r: {$ref: "#/$defs/stringOrArray"},
m: {$ref: "#/$defs/stringOrArray"},
c: {$ref: "#/$defs/stringOrArray"},
x: {
type: "string",
},
errors: {enum: ["json", "line", "text", "js", "no"]},
changes: {enum: [true, "json", "line", "js"]},
spec: {enum: ["draft7", "draft2019", "draft2020", "jtd"]},
Expand All @@ -31,7 +34,7 @@ export default cmd
function execute(argv: ParsedArgs): boolean {
const ajv = getAjv(argv)
const validate = compile(ajv, argv.s)
return getFiles(argv.d)
return getFiles(argv.d, argv.x)
.map(validateDataFile)
.every((x) => x)

Expand Down

0 comments on commit 6f0fe51

Please sign in to comment.