Giter Site home page Giter Site logo

ajv-validator / ajv-pack Goto Github PK

View Code? Open in Web Editor NEW
43.0 43.0 8.0 41 KB

๐Ÿšจ[ARCHIVED] Produces a compact module exporting JSON-schema validation functions compiled by Ajv

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

License: MIT License

JavaScript 90.86% HTML 9.14%
ajv compiler json-schema validator

ajv-pack's People

Contributors

epoberezkin avatar haines avatar sapegin avatar timstott 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

Watchers

 avatar  avatar  avatar  avatar  avatar

ajv-pack's Issues

anyOf with non recursive reference crashes

This simple example is crashing for me. I included the version numbers in the comments, and a note about how the order of items in "anyOf" affects the result. The example has two schemas where schema2 references schema1 and schema 1 references itself. Let me know if I am mistaken, but it seems like this does not violate the limitation:
"recursive references (reference to the current schema { "$ref": "#" } is supported)."
Thanks!

var Ajv = require('ajv'); // version 5.5.1
var ajv = new Ajv({sourceCode: true});
var pack = require('ajv-pack'); // version 0.3.1

var schema1 = { // references self
        "coordinate": {
            "type": "number"
        },
        "position": {
            "type": "array",
            "items": { "$ref": "#/coordinate" },
            "minItems": 3,
            "maxItems": 3
        }
};

var schema2 = { // references schema1
  "point": {
      "type": "object",
      "properties": {
          "point":       { "$ref": "types#/position" }
      }
  },
  "circle": {
      "type": "object",
      "properties": {
          "origin": {
              "anyOf": [ // Swapping the order of this list allows it to not crash
                  { "$ref": "types#/position" },
                  { "$ref": "#/point" }
              ]
          },
      }
  }
};

ajv.addSchema(schema1, "types");
ajv.addSchema(schema2, "entity");
validate = ajv.compile({ $ref: "entity#/circle" });
var moduleCode = pack(ajv, validate);
console.log(moduleCode);

Schemas with definitions

This schema:

{
  "type": "object",
  "properties": {
    "foo": { "$ref": "#/definitions/foo" }
  },
  "definitions": {
    "foo": { "type": "number" }
  }
}

with option {inlineRefs: false} cannot be saved as module (or any schema where refs cannot be inlined, even if there is no recursion).

RangeError: Maximum call stack size exceeded

Make js-beautify dependency optional

It would be nice if the dependencies on js-beautify was optional as it brings quite a few transitive dependencies:

โ”œโ”€โ”ฌ [email protected]
โ”‚ โ”œโ”€โ”ฌ [email protected]
โ”‚ โ”‚ โ”œโ”€โ”€ [email protected]
โ”‚ โ”‚ โ””โ”€โ”€ [email protected]
โ”‚ โ”œโ”€โ”ฌ [email protected]
โ”‚ โ”‚ โ”œโ”€โ”€ [email protected]
โ”‚ โ”‚ โ”œโ”€โ”ฌ [email protected]
โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ [email protected]
โ”‚ โ”‚ โ”‚ โ””โ”€โ”€ [email protected]
โ”‚ โ”‚ โ”œโ”€โ”€ [email protected]
โ”‚ โ”‚ โ””โ”€โ”€ [email protected]
โ”‚ โ”œโ”€โ”€ [email protected] deduped
โ”‚ โ”œโ”€โ”€ [email protected]
โ”‚ โ””โ”€โ”ฌ [email protected]
โ”‚   โ””โ”€โ”€ [email protected]

and it's not strictly required for producing valid ES module output.

enum doesn't work when in external referenced schema

Example:

const Ajv = require('Ajv');
const pack = require('ajv-pack');
const requireFromString = require('require-from-string');

let dep = {
  "$schema": "http://json-schema.org/draft-04/schema#",
  "foo": {
    "type": "integer",
    "enum": [1, 2]
  }
};

let moduleCode;
let ajv = new Ajv({ sourceCode: true });
ajv.addSchema(dep, 'definitions.json');
// No ref - all good
let validate = ajv.compile({
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "integer",
  "enum": [1, 2]
});
console.log(`Valid: ${validate(1)}`);                // => true
console.log(`Invalid: ${validate(0)}`);              // => false
moduleCode = pack(ajv, validate);
let packedValidate = requireFromString(moduleCode);
console.log(`Valid packed: ${packedValidate(1)}`);   // => true
console.log(`Invalid packed: ${packedValidate(0)}`); // => false

// With ref - some bad
let validate_ref = ajv.compile({
  "$schema": "http://json-schema.org/draft-04/schema#",
  "$ref": "definitions.json#/foo"
});

console.log(`Valid ref: ${validate_ref(1)}`);              // => true
console.log(`Invalid ref: ${validate_ref(0)}`);            // => false
moduleCode = pack(ajv, validate_ref);
let packedValidate_ref = requireFromString(moduleCode);
console.log(`Valid ref packed: ${packedValidate_ref(1)}`); // => error
console.log(`Invalid ref packed: ${packedValidate_ref(0)}`);

The error I receive:

PS C:\ajv-minimal-example> node index
Valid: true
Invalid: false
Valid ref: true
Invalid ref: false
Valid packed: true
Invalid packed: false
:12
    var schema1 = refVal1.enum;
                  ^

ReferenceError: refVal1 is not defined
    at validate (<anonymous>:12:19)
    at Object.<anonymous> (C:\ajv-minimal-example\index.js:42:34)
    at Module._compile (module.js:541:32)
    at Object.Module._extensions..js (module.js:550:10)
    at Module.load (module.js:458:32)
    at tryModuleLoad (module.js:417:12)
    at Function.Module._load (module.js:409:3)
    at Function.Module.runMain (module.js:575:10)
    at startup (node.js:160:18)
    at node.js:456:3

After some testing, things that aren't issues:

  1. Refs without enum, e.g. just "type": "integer"
  2. (as seen above) the validate function for an enum-containing-ref prior to being packed
  3. (as seen above) the packed validate function for an enum-containing schema

For reference here are my dependencies when running the above example:

    "ajv": "^4.9.2",
    "ajv-pack": "^0.2.7",
    "require-from-string": "^1.2.1"

And I'm using node v6.2.1.

Generated module requires ajv/lib/compile/equal

Hey there! Thank you for your work on ajv.

I was trying to generate a standalone validation module using ajv-cli version 3.0.0. (The schema I'm trying to compile should be simple enough to not hit any limitations mentioned in the README.)

$ ajv compile -s schema.json -o validation.js

schema schema.json is valid

The tool generates a module that begins with:

'use strict';
var equal = require('ajv/lib/compile/equal');
var validate = (function() {
  var refVal = [];
  return function validate(data, dataPath, parentData, parentDataProperty, rootData) {
[...]

Trying to import this module in Node (v12.2) results in the following error being thrown:

> var validation = require('./validation.js')
Thrown:
Error: Cannot find module 'ajv/lib/compile/equal'

I expected the generated module to be self-contained, with no external dependencies. Have I misunderstood something, or is this a bug?

const with $data does not resolve value for equality

Hi,

Defining a const rule with $data does not work using ajv-pack.

This schema:

{
  "type": "object",
  "properties": {
    "password": {
      "type": "string",
      "minLength": 1
    },
    "confirmPassword": {
      "const": {"$data": "1/password"}
    }
  },
  "required": [
    "password",
    "confirmPassword"
  ]
}

Results in this code (relevant part only), and the --data flag was used in the cli command:

var schema1 = validate.schema.properties.confirmPassword.const;
var valid1 = equal(data.confirmPassword, schema1);

where equal is a deep-equal function. The $data is never resolved, the const value used for the equality check is 1/password.

Is this a ajv-pack bug?

Thanks, Adam.

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.