GithubHelp home page GithubHelp logo

ajv-validator / ajv-cli Goto Github PK

View Code? Open in Web Editor NEW
257.0 257.0 66.0 197 KB

Command-line interface for Ajv JSON Validator

Home Page: https://ajv.js.org

License: MIT License

JavaScript 3.47% TypeScript 96.53%
ajv command-line json-schema validator

ajv-cli's People

Contributors

agh42 avatar andreineculau avatar bauglir avatar cyplo avatar dependabot-preview[bot] avatar ehmicky avatar epoberezkin avatar fedorov avatar g-rath avatar jessedc avatar kleewho avatar notjosh avatar ralfhandl avatar relequestual avatar rvanider avatar xmedeko avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

ajv-cli's Issues

Does ajv-cli tool validates the format type of specification.

Hello All,
Given below is the specification in which we have type and format of fields. I am able to validate the specification with type parameter with ajv-cli but I also want to validate with format parameter. Is there a way to validate specification with format parameter?
"schema"{
"properties": {
"startDate": {
"type": "string",
"format": "date"
},
"storage": {
"type": "number",
"format": "float"
}
}

Can't $ref relative file.

I'm having trouble using a parent schema that references a child schema via a relative URL.

Here's my parent schema:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "id": "parent.json",
  "description": "The parent schema.",
  "type": "object",
  "properties": {
    "parentProp1": { "type": "string"},
    "parentProp2": { "$ref": "child.json#"}
  }
}

Here's my child schema:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "description": "The parent schema.",
  "type": "object",
  "properties": {
    "childProp1": { "type": "number" }
  }
}

Here's my data:

{
  "parentProp1": "one",
  "parentProp2": {
    "childProp1": 1
  }
}

Here's the error I get:

$ ajv -s parent.json -d parent-test.json
schema parent.json is invalid
error: can't resolve reference child.json# from id parent.json#

Does not validate from external schema

When I try to compile the attached schema comment.schema.json, it fails with error message

schema comment.schema.json is invalid
error: can't resolve reference user.schema.json from id #

Both comment.schema.json and user.schema.json are located in the same directory, where also ajv is invoked.

The same schema validates with jayschema (which is what is to be expected because the respective $ref value does not contain any # character, so it does not refer to the current schema document).

I also tried "./user.schema.json" with the same result: does not validate in ajv but it does in jayschema.

Recognizing the hyper-schema meta-schema?

Similar to #28, would it be possible to automatically recognize http://json-schema.org/draft-04/hyper-schema and http://json-schema.org/draft-06/hyper-schema as built-in supported meta-schemas?

(venv) HenrysMacBookPro:schemas henry$ ajv compile -s common/user.json
schema common/user.json is invalid
error: no schema with key or ref "http://json-schema.org/draft-04/hyper-schema#"
(venv) HenrysMacBookPro:schemas henry$ curl http://json-schema.org/draft-04/hyper-schema > hyper-schema
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  4639  100  4639    0     0  21803      0 --:--:-- --:--:-- --:--:-- 21882
(venv) HenrysMacBookPro:schemas henry$ ajv compile -m hyper-schema -s common/user.json
schema common/user.json is valid
(venv) HenrysMacBookPro:schemas henry$ 

Specify a glob

The ideal CLI would look something like ajv "**/*.json" -s schema.json. Note that the glob is in a string so that the tool will handle globs and Windows can be supported.

Self referencing schemas are not compile-able

Hi,
My schema looks something like this: (look below - specifically at steps)
It has two references to itself but I can't seem to be able to compile the schema (or run a validation process of a json against it). I also tried adding --v5 with no avail.

Is this fixable?

{
  "type": "object",
  "id": "dialog",
  "properties": {
    "steps": { "$ref": "#/definitions/steps" },
    }
  },
  "definitions": {
    "steps": {
      "id": "steps",
      "type": "array",
      "items": {
        "type": "object",
        "oneOf": [
          {
            "id": "step",
            "properties": {
              "scenarios": {
                "type": "array",
                "items": {
                  "type": "object",
                  "properties": {
                    "steps": {
                      "$ref": "#/definitions/steps"
                    }
                  }
                }
              }
            }
          },
          { ... }
        ]
     }
  }
}

Provide directory watcher flag to run validation when files change

Feature Request:

Create a flag (e.g. --watch) that watches the target files for changes and re-runs the validation automatically when they occur.

The idea being to have a separate window open and running that shows immediate validation results ever time a change to the schema or data files is detected.

GitLab CI not possible when more than one JSON file for validation is found

I've prepared a very simple GitLab repo to show this problem: https://gitlab.com/sukhoi191/ajv-cli-pipeline-test

Basically, when these criteria are met:

  1. GitLab CI configuration has step similar to this one: ajv -s __documentation__/json-schema/Blacklist.schema.json -d __data__/*Blacklist.json (see: https://gitlab.com/sukhoi191/ajv-cli-pipeline-test/blob/master/.gitlab-ci.yml)
  2. In __data__ directory, there is more than one file that matches the wildcard (e.g. SampleBlacklist.json and SampleAnotherBlacklist.json, see: https://gitlab.com/sukhoi191/ajv-cli-pipeline-test/tree/master/__data__).

GitLab pipeline fails on executing ajv with message:

$ ajv -s __documentation__/json-schema/Blacklist.schema.json -d __data__/*Blacklist.json
Unknown command __data__/SampleBlacklist.json

Example: https://gitlab.com/sukhoi191/ajv-cli-pipeline-test/-/jobs/325663668

When I run this command locally on my computer, it works without any problems.

I'm not sure if this is more ajv-cli issue, or maybe GitLab one.

Passing a JSON Pointer with a schema

I'm looking at using ajv-cli to help test Hyper-Schema requests and responses. This means that the schema I want to validate against is not always the entire schema file.

What would you think of allowing something like:

ajv validate -s "foo.json#/links/0/targetSchema" -d "data.json"
or with .json omitted:
ajv validate -s "foo#/links/0/targetSchema" -d "data"

It would, of course, not be too hard to write some other script to pull out the appropriate schema, and that might be better given that selecting a link by numeric index is pretty fragile. So I'm not sure this is the best solution but thought I'd float it .

Add draft7 migration

ajv 6.0.0 likes draft7 and suggests using ajv-cli to migrate.

ajv-cli claims to be updated for ajv 6.0.0 but the migrate command migrates no farther than draft6.

Custom error messages are excluded in compiled schemas

Compiling the following schema:

...
"properties": {
  "foo": {
    "type": "string",
    "minLength":  "10",
    "errorMessage": {
      "minLength": "foo must be at least 10 characters long."
    }
  }
}
...

results in:

...
var err = {
  keyword: 'minLength',
  dataPath: (dataPath || '') + '/foo',
  schemaPath: '#/properties/foo/minLength',
  params: {
    limit: 10
  },
  message: 'should NOT be shorter than 10 characters'
};
...

err.message should match properties.foo.errorMessage.

Perhaps this can be solved with an extra command line option that will augment the underlying ajv instance with ajv-error.

ajv compile not failing if required properties are not defined

ajv compile -s /tmp/ajv.json does not fail where the required properties are not
defined in the schema and additionalProperties is false.

file /tmp/ajv.json:

{
  "$schema": "http://json-schema.org/draft-06/schema#",
  "id": "https://api.apiture.com/schemas/audit/authorizedSignerInvited/v1.0.0/schema.json",
  "type": "object",
  "description": "Sample schema with invalid \"required\" aray",
  "additionalProperties": false,
  "required": [
    "aa"
  ],
  "properties": {
    "a": {
      "description": "value a",
      "type": "string"
    },
    "aaa": {
      "description": "value aaa",
      "type": "string"
    }
  }
}

compile it:

$ ajv compile -s /tmp/ajv.json
schema /tmp/ajv.json is valid

I believe this should fail

`ajv compile` command: add schemas before compiling them to allow refs between schemas

If you use ajv-cli to compile multiple schemas, it appears to process them in alphabetical order. If one of the earlier schemas references a later schema, the compilation will fail because the later schema has not yet been validated. You can work around that (appropriately) by adding the -r '**/*.json' flag so that all the referenced schemas are added to AJV first.

This seems non-intuitive (at least it tripped me up for an hour) because you can have some folders of schemas that work just fine, and then just by adding a reference to a schema that comes later in the alphabetical order, the schema resolution seems to stop working.

My suggestion would be that if a glob is passed to the compile step, that all schemas in the glob are first added to AJV, and then they are compiled after that, so that the order of them appearing in the glob does not matter, and users do not have to reference them again using the -r flag.

For example:

  1. Take the attached folder of schemas and untar it on your workstation.
  2. From the untarred schemas folder, run ajv compile -s '**/*.json'.
  3. You'll see that auth-auth/token-with-nonce.json failed because it couldn't resolve a schema it depended on (that came later in the glob).
  4. mv auth-auth zzzz
  5. Run ajv compile -s '**/*.json' again. Everything worked this time because the dependencies happened to be in the right order.

Here's the output of that exercise:

jrthomer@work:/tmp/foo/schemas$ ajv compile -s '**/*.json'
schema auth-auth/token-with-nonce.json is invalid
error: can't resolve reference ../core/jwt.json from id https://example.com/schemas/auth-auth/token-with-nonce.json#
schema core/jwt.json is valid
schema core/uuid.json is valid

jrthomer@work:/tmp/foo/schemas$ mv auth-auth zzzz

jrthomer@work:/tmp/foo/schemas$ ajv compile -s '**/*.json'
schema core/jwt.json is valid
schema core/uuid.json is valid
schema zzzz/token-with-nonce.json is valid

And if I had specified the glob twice, it would've worked (with the original folder structure):

jrthomer@work:/tmp/foo/schemas$ ajv compile -r '**/*.json' -s '**/*.json'
schema auth-auth/token-with-nonce.json is valid
schema core/jwt.json is valid
schema core/uuid.json is valid

If you don't agree with the proposed solution of always doing the addSchema step on all schemas before doing the compile step, perhaps at least the documentation could emphasize the need for the -r flag, which may be missed by newcomers (at least I missed it, and didn't realize it until I read the AJV code and made my own test case using AJV directly).

Thanks!

can't resolve relative refs on disk

First thanks for this awesome tool.

Though I can't get relative urls pass for validation.
Maybe the reason is that I don't serve them right now, just using them locally on disk ๐Ÿค”

> ajv compile -s 'schemas/**/*.json'

schema schemas/api-schema.json is invalid
error: can't resolve reference bpmData.json# from id #
schema schemas/bpmData.json is invalid
error: can't resolve reference ./types/pageValues.json# from id http://www.axa.ch/bpmData.json#
schema schemas/components/Autocomplete.json is invalid
error: can't resolve reference ../types/bpmid.json# from id http://www.axa.ch/components/Autocomplete.json#
schema schemas/components/BulletRadioButtons.json is invalid
error: can't resolve reference ../types/bpmid.json# from id http://www.axa.ch/components/BulletRadioButtons.json#
schema schemas/components/Button.json is invalid
error: can't resolve reference ../types/bpmid.json# from id http://www.axa.ch/components/Button.json#
schema schemas/components/Checkbox.json is invalid
error: can't resolve reference ../types/bpmid.json# from id http://www.axa.ch/components/Checkbox.json#
schema schemas/components/Datepicker.json is invalid
error: can't resolve reference ../types/bpmid.json# from id http://www.axa.ch/components/Datepicker.json#
schema schemas/components/Dropdown.json is invalid
error: can't resolve reference ../types/bpmid.json# from id http://www.axa.ch/components/Dropdown.json#
schema schemas/components/Error.json is invalid
error: schema with key or id "http://www.axa.ch/components/Autocomplete.json" already exists
schema schemas/components/Icon.json is invalid
error: can't resolve reference ../types/component.json# from id http://www.axa.ch/components/Icon.json#
schema schemas/components/Link.json is invalid
error: can't resolve reference ../types/bpmid.json# from id http://www.axa.ch/components/Link.json#
schema schemas/components/List.json is invalid
error: can't resolve reference ../types/component.json# from id http://www.axa.ch/components/List.json#
schema schemas/components/ListItem.json is invalid
error: schema is invalid: data.properties['additionalProperties'] should be object
schema schemas/components/Map.json is invalid
error: can't resolve reference ../types/component.json# from id http://www.axa.ch/components/Map.json#
schema schemas/components/Markdown.json is invalid
error: can't resolve reference ../types/component.json# from id http://www.axa.ch/components/Markdown.json#
schema schemas/components/ModalDialog.json is invalid
error: can't resolve reference ../types/bpmid.json# from id http://www.axa.ch/components/ModalDialog.json#
schema schemas/components/MultipleSelectList.json is invalid
error: can't resolve reference ../types/bpmid.json# from id http://www.axa.ch/components/MultipleSelectList.json#
schema schemas/components/MultipleSelectListItem.json is invalid
error: can't resolve reference ../types/component.json# from id http://www.axa.ch/components/MultipleSelectListItem.json#
schema schemas/components/ProcessEnding.json is invalid
error: can't resolve reference ../types/bpmid.json# from id http://www.axa.ch/components/ProcessEnding.json#
schema schemas/components/SegmentedRadioButtons.json is invalid
error: can't resolve reference ../types/bpmid.json# from id http://www.axa.ch/components/SegmentedRadioButtons.json#
schema schemas/components/SegmentedRadioButtonsItem.json is invalid
error: schema is invalid: data.properties['additionalProperties'] should be object
schema schemas/components/SelectList.json is invalid
error: can't resolve reference ../types/bpmid.json# from id http://www.axa.ch/components/SelectList.json#
schema schemas/components/SelectListItem.json is invalid
error: can't resolve reference ../types/component.json# from id http://www.axa.ch/components/SelectListItem.json#
schema schemas/components/Textarea.json is invalid
error: can't resolve reference ../types/bpmid.json# from id http://www.axa.ch/components/Textarea.json#
schema schemas/components/Textfield.json is invalid
error: can't resolve reference ../types/bpmid.json# from id http://www.axa.ch/components/Textfield.json#
schema schemas/components/Typo.json is invalid
error: can't resolve reference ../types/component.json# from id http://www.axa.ch/components/Typo.json#
schema schemas/components/Wizard.json is invalid
error: can't resolve reference ../types/component.json# from id http://www.axa.ch/components/Wizard.json#
schema schemas/error.json is valid
schema schemas/schema.json is invalid
error: can't resolve reference ../types/component.json# from id http://www.axa.ch/components/Wizard.json#
schema schemas/types/bpmid.json is valid
schema schemas/types/children.json is invalid
error: can't resolve reference ../types/component.json# from id http://www.axa.ch/components/Autocomplete.json#
schema schemas/types/component.json is invalid
error: schema is invalid: data.enum should NOT have less than 1 items
schema schemas/types/controls.json is invalid
error: can't resolve reference ../types/component.json# from id http://www.axa.ch/components/Button.json#
schema schemas/types/event.json is valid
schema schemas/types/msg.json is valid
schema schemas/types/pageValues.json is valid
schema schemas/validators/confirmation.json is valid
schema schemas/validators/email.json is valid
schema schemas/validators/format.json is valid
schema schemas/validators/length.json is valid
schema schemas/validators/required.json is valid

Summary for multiple files

It would be great if the CLI gave a summary when it is run over multiple data files. Currently it just prints one line (<filename> valid) for each file.

Something like the test runners do:

Analysed 30 files. 28 valid. 2 invalid.

Compiled validate function of schema for JSONSchema appears to be invalid

I am using JSON schema's schema renamed to schema.json to generate a validator:

ajv compile -s ./schema.json -o jsonSchemaValidator.js

The output jsonSchemaValidator.js attempts to reference refVal5.enum. But refVal5 is undefined... so when I try to use the exported validate function I get an error about it being undefined regardless of the schema I pass to it.

For example in a node repl:

var validate = require('./jsonSchemaValidator');
var fs = require('fs')
var schemaString = fs.readFileSync('./schema.json', 'utf8');
validate(JSON.parse(schemaString));

And I get this exception:

ReferenceError: refVal5 is not defined
    at validate (/home/dparker/src/test/jsonSchemaValidator.js:1143:79)
    at validate (/home/dparker/src/test/jsonSchemaValidator.js:841:64)
    at repl:1:1
    at sigintHandlersWrap (vm.js:32:31)
    at sigintHandlersWrap (vm.js:96:12)
    at ContextifyScript.Script.runInContext (vm.js:31:12)
    at REPLServer.defaultEval (repl.js:308:29)
    at bound (domain.js:280:14)
    at REPLServer.runBound [as eval] (domain.js:293:12)
    at REPLServer.<anonymous> (repl.js:489:10)

I did npm install --save-dev ajv so that the validate function can import formats and equals.

I am assuming compiling a validator for json schema should work and I am making a mistake. Any suggestions?

I don't see bugs about it in https://github.com/epoberezkin/ajv Before I file a bug there I wanted to see if I am using the cli correctly. Thank you in advance for support/help.

Validate against draft-04?

ajv lets you load the draft-04 meta-schema. Is there a way to do this in the cli? If I try to add it using -m it can't find it because it's trying to find the schema on my local filesystem.

$ ajv compile -s "schemas/myschema.json" -m "http://json-schema.org/draft-04/schema#"
$ error:  Cannot find meta-schema '/home/****/projects/my-schemas/http:/json-schema.org/draft-04/schema#'

Show line number in file where error is

Hi. I ran a validation and got quite a few errors saying "data[0] should NOT have additional properties". However with no line numbers it's quite hard to work out what field is actually extra.

I think it would be a nice feature to also say what line in the file the error is referring to.

PS: Nice tool.

Exclude file

Is there an option to exclude one specific file?

I have a list of random file names ending in *.json from which I need to exclude one specific one, in my case manifest.json.

Feature Request: resolve and extract

e.g.

ajv resolve https://gist.githubusercontent.com/mnpenner/e10466e8f8993994e04cdace3a233f72/raw/d4adb168285e220e612aef579451a5a868853cbe/schema.json#defs/Table

Should parse the given schema and resolve/inline all $refs and then output a new JSON schema with the given path (#defs/Table) moved up the root. Unused sub-definitions can be discarded if needed.

e.g. the output should look something like:

{
    "type": "object",
    "additionalProperties": false,
    "properties": {
        "name": {
            "$ref": "#/defs/Identifier"
        },
        "versions": {
            "type": "array",
            "items": {
                "$ref": "#/defs/TableVersion"
            }
        }
    },
    "required": [
        "name",
        "versions"
    ]
}

But with those two $refs resolved.

This would make it easy to uncombine and export sub-schemas for different applications.

N.B. resolving remote URLs isn't necessary, I just used that as an example. Path to schema on disk is fine.

Default schema and data files

This recently came up in discussion in the exercism repo.

CLI should be able to auto-detect source and data files with some standard name in case no arguments are passed. Maybe schema.json and data.json?
This way if I have these two files, I can just run ajv directly. If filenames are different than default, I shall pass them as arguments to -s and -d.

Question: referencing definition imported with -r option from another file

Opening a new issue to avoid spamming #29. May be a pretty n00bish question :)

From what I read from the documentation I should be able to load schemas with the -r option, and the expected behaviour is that "The schema in -s parameter can reference any of these schemas with $ref keyword.".
However, I'm not currently able to correctly reference the schema in the file.

I'm running the cli like this: ajv validate -r schemas/definitions.json -s schemas/devices-schema.json -d devices/devices.json, where schemas/definitions.json contains only one definition:

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "id": "http://belka.us/schemas/definitions.json",
  "definitions": {
    "address": {
      "type": "string",
      "description": "An hardware address in hexadecimal format",
      "pattern": "^0x[0-9A-F]{4}$"
    }
  }
}

I would then like to use that definition in devices-schema.json, but I'm failing in finding the correct syntax to do so.

"properties": {
        "version":        { "$ref": "/definitions.json/definitions/address" },

Accept data on stdin

The most common way I've noticed many people do casual testing of API responses is with curl.
In particular some back-end devs really prefer the command line, which makes ajv-cli great for this purpose. Would it be possible for ajv-cli to take the data JSON from stdin so that

$ curl -X PUT "https://api.example.com/foos/506e3185e9c882d175a2d0cb0093d9f2" \
       -H "Content-Type: application/json" \
       --data '{"id":"506e3185e9c882d175a2d0cb0093d9f2","bar":"1234"}' \
       | ajv validate -s foos-response.json

works? And likewise for ajv test. This of course is more to allow people to poke at things with minimal setup than something intended for a large scale test automation / CI system.

Allow ajv option addUsedSchema

For the JSON Schema site, our build / test process includes validating that all example schemas are valid according to the meta-schema.

We update the meta-schema, and now have a failure in our build, because by default when calling validate, ajv will try to add the schema to its index, and throws an error if the $id already exists.

json-schema-org/json-schema-org.github.io#308

We merged anyway because we know it's fine, but it would be nice to have future builds passing OK without the false positive.

I'll look to PR this week.

data filename must not end in .json (+ security)

you make use of nodejs' require, presumably for the sake of omit-extension convenience, but this means that ajv-cli cannot be use to validate an AWS Cloudformation *.template instance, or .eslintrc, etc i.e. files that are JSON formatted, but don't end in .json

PS: also, using nodejs' require, is major security issue imho. I build some service to validate JSON, someone passes down a js module instead, that does evil things, but still exports a JSON instance. Everything works fine still, but the system has been tempered with.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.