ajv-validator / ajv-pack Goto Github PK
View Code? Open in Web Editor NEW๐จ[ARCHIVED] Produces a compact module exporting JSON-schema validation functions compiled by Ajv
Home Page: https://ajv.js.org
License: MIT License
๐จ[ARCHIVED] Produces a compact module exporting JSON-schema validation functions compiled by Ajv
Home Page: https://ajv.js.org
License: MIT License
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);
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
To allow for mutually recursive references and to reduce total code size (in case two schemas reference the same schema)
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.
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:
enum
, e.g. just "type": "integer"
validate
function for an enum
-containing-ref prior to being pack
edvalidate
function for an enum
-containing schemaFor 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.
formats, equal, ucs2length can be removed if unused
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?
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.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.