Open viebel opened 3 years ago
To use in your client app you can still use Ajv as part of your bundle. Using a separate bundle is only helpful if your app is very large and split across multiple bundles, to avoid duplication, but even then you can include it in the first bundle and expose as a browser global for other bundles.
Which file to use depends on the schema version. I’ve just looked at the list, ajv that supports draft-2020-12 was never published to CDN, needs to be added.
What script exactly should I load from the CDN? ajv.2019.min.js or ajv.2019.bundle.js?
.min - the other file is non-minified version. What exactly didn’t work?
Here is the error I am getting in the browser console when load the script at https://cdnjs.cloudflare.com/ajax/libs/ajv/8.6.1/ajv7.min.js
Uncaught ReferenceError: module is not defined
at ajv7.min.js:9
at ajv7.min.js:1
at ajv7.min.js:1
This is a JS Fiddle that reproduces the issue: https://jsfiddle.net/viebel/qw1d0pom/2/
Notice that my real use case is not for a client app. I need to embed ajv in a HTML web page as a script tag.
I see - I think I know what could be the problem, which means nobody uses the bundles lately, as it wasn’t reported before.
I also think I know how to fix it, but it would be blocked by #1759 (the part about ajv-dist, website is unrelated, probably should have been two issues)…
So if you have time to investigate / fix what’s broken in CI for ajv-dist, I could fix this particular issue quickly, otherwise it’ll be couple weeks I’m afraid, it doesn’t seem a high priority…
Thank you for the clarification. I don't have time right now to investigate #1759 FWIW, I discovered that the highest version from the CDN that works is 6.12.6. I could live with that although I'd be more than happy to upgrade to the latest Ajv version.
Great library by the way!
the highest version from the CDN that works is 6.12.6.
yes, that’s what I thought it could be
We just upgraded to draft-2020-12 based on confidence using server side Ajv8 and now our webapps don't work. When can we see 2020-12 handling or higher on CDN? Since we generate our schema, it's not too bad to flip between schema versions, but changing HTML files can be somewhat painful. The 2020-12 or 8.6.3 is hardcoded in around 28 places in one module, but we have other modules. Can I create a bundle on my own system? I was trying to bundle the node_modules/ajv, but it wasn't working, so I'll probably try to check out from github as a test in a separate folder. Thanks! @brutzman see that I informed Ajv folks. It appears to be some bundling/release that needs to be done.
I'm not sure I want to touch Ajv 8.6.3 yet. There are 44 moderate to high vulnerabilities in dependencies (probably in devDependencies, so they don't particularly show up as real dependencies in an including project.. @brutzman I can't even create a package-lock.json file when i clone it--but following directions seemed to have fixed that.
So I can create the following bundles:
~/ajv/bundle$ ls
ajv2019.bundle.js ajv2019.min.js ajv2019.min.js.map ajv7.bundle.js ajv7.min.js ajv7.min.js.map ajvJTD.bundle.js ajvJTD.min.js ajvJTD.min.js.map
Note, no 2020 bundles, but I do see 2020 distributions:
~/ajv/dist$ ls 2020.*
2020.d.ts 2020.js 2020.js.map
I also note that 2020.js uses require, so we'll need a way to include dependencies. Bundles should avoid that. I the best approach at this point is to wait a few weeks to see if this issue gets dealt with, or pursue bundling for 2020. That will require some research.
Good news, @brutzman, I was able to produce dist and bundle files using this patch:
~/ajv$ git diff .
diff --git a/rollup.config.js b/rollup.config.js
index d435b70f..09dafef1 100644
--- a/rollup.config.js
+++ b/rollup.config.js
@@ -28,5 +28,6 @@ function createBundleConfig(sourceFile, outFile, globalName) {
export default [
createBundleConfig("ajv", "ajv7", "av7"),
createBundleConfig("2019", "ajv2019", "ajv2019"),
+ createBundleConfig("2020", "ajv2020", "ajv2020"),
createBundleConfig("jtd", "ajvJTD", "ajvJTD"),
]```
So now I have 2020 dist and bundles to play with! I don't know how I managed to find the config file, it just kind of happened.
Test results:
$ npm run test-all
> ajv@8.6.3 test-all
> npm run test-cov && if-node-version 12 npm run test-browser
> ajv@8.6.3 test-cov
> nyc npm run test-spec
> ajv@8.6.3 test-spec
> cross-env TS_NODE_PROJECT=spec/tsconfig.json mocha -r ts-node/register "spec/**/*.spec.{ts,js}" -R dot -g \(.recursiveRef.with.no..recursiveAnchor\|.dynamicRef.with.no..dynamicAnchor\).in.the.initial.target.schema.resource -i
Error: Cannot find module './_json/async'
Require stack:
- /home/coderextreme/ajv/spec/async_schemas.spec.ts
- /home/coderextreme/ajv/node_modules/mocha/lib/nodejs/esm-utils.js
- /home/coderextreme/ajv/node_modules/mocha/lib/mocha.js
- /home/coderextreme/ajv/node_modules/mocha/lib/cli/one-and-dones.js
- /home/coderextreme/ajv/node_modules/mocha/lib/cli/options.js
- /home/coderextreme/ajv/node_modules/mocha/bin/mocha
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
at Function.Module._resolveFilename.sharedData.moduleResolveFilenameHook.installedValue [as _resolveFilename] (/home/coderextreme/ajv/node_modules/@cspotcode/source-map-support/source-map-support.js:679:30)
at Function.Module._load (node:internal/modules/cjs/loader:778:27)
at Module.require (node:internal/modules/cjs/loader:1005:19)
at require (node:internal/modules/cjs/helpers:94:18)
at Object.<anonymous> (/home/coderextreme/ajv/spec/async_schemas.spec.ts:15:29)
at Module._compile (node:internal/modules/cjs/loader:1101:14)
at Module.replacementCompile (/home/coderextreme/ajv/node_modules/append-transform/index.js:60:13)
at Module.m._compile (/home/coderextreme/ajv/node_modules/ts-node/src/index.ts:1365:23)
at module.exports (/home/coderextreme/ajv/node_modules/default-require-extensions/js.js:7:9)
at /home/coderextreme/ajv/node_modules/append-transform/index.js:64:4
at require.extensions.<computed> (/home/coderextreme/ajv/node_modules/ts-node/src/index.ts:1368:12)
at Object.<anonymous> (/home/coderextreme/ajv/node_modules/append-transform/index.js:64:4)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Module.require (node:internal/modules/cjs/loader:1005:19)
at require (node:internal/modules/cjs/helpers:94:18)
at Object.exports.requireOrImport (/home/coderextreme/ajv/node_modules/mocha/lib/nodejs/esm-utils.js:56:20)
at async Object.exports.loadFilesAsync (/home/coderextreme/ajv/node_modules/mocha/lib/nodejs/esm-utils.js:88:20)
at async singleRun (/home/coderextreme/ajv/node_modules/mocha/lib/cli/run-helpers.js:125:3)
at async Object.exports.handler (/home/coderextreme/ajv/node_modules/mocha/lib/cli/run.js:374:5)
=============================== Coverage summary ===============================
Statements : 25.08% ( 759/3026 )
Branches : 10.82% ( 197/1820 )
Functions : 7.98% ( 53/664 )
Lines : 25.9% ( 752/2903 )
================================================================================
More test results:
~/ajv$ npm run test-browser
> ajv@8.6.3 test-browser
> rm -rf .browser && npm run bundle && scripts/prepare-tests && karma start
> ajv@8.6.3 bundle
> rm -rf bundle && rollup -c
./lib/ajv.ts → ./bundle/ajv7.bundle.js, ./bundle/ajv7.min.js...
(!) Cannot call a namespace ('traverse')
lib/compile/resolve.ts (95:4)
93: const localRefs = {};
94: const schemaRefs = new Set();
95: traverse(schema, { allKeys: true }, (sch, jsonPtr, _, parentJsonPtr) => {
^
96: if (parentJsonPtr === undefined)
97: return;
(!) Cannot call a namespace ('equal')
lib/compile/resolve.ts (137:35)
135: return localRefs;
136: function checkAmbiguosRef(sch1, sch2, ref) {
137: if (sch2 !== undefined && !equal(sch1, sch2))
^
138: throw ambiguos(ref);
139: }
(!) Illegal reassignment to import 'equal'
lib/runtime/equal.ts (3:0)
1: // https://github.com/ajv-validator/ajv/issues/889
2: import * as equal from "fast-deep-equal";
3: equal.code = 'require("ajv/dist/runtime/equal").default';
^
4: export default equal;
5: //# sourceMappingURL=equal.js.map
(!) Mixing named and default exports
https://rollupjs.org/guide/en/#outputexports
The following entry modules are using named and default exports together:
lib/ajv.ts
lib/ajv.ts
Consumers of your bundle will have to use chunk['default'] to access their default export, which may not be what you want. Use `output.exports: 'named'` to disable this warning
(!) Plugin typescript: @rollup/plugin-typescript: Rollup 'sourcemap' option must be set to generate source maps.
created ./bundle/ajv7.bundle.js, ./bundle/ajv7.min.js in 5.2s
./lib/2019.ts → ./bundle/ajv2019.bundle.js, ./bundle/ajv2019.min.js...
(!) Cannot call a namespace ('traverse')
lib/compile/resolve.ts (95:4)
93: const localRefs = {};
94: const schemaRefs = new Set();
95: traverse(schema, { allKeys: true }, (sch, jsonPtr, _, parentJsonPtr) => {
^
96: if (parentJsonPtr === undefined)
97: return;
(!) Cannot call a namespace ('equal')
lib/compile/resolve.ts (137:35)
135: return localRefs;
136: function checkAmbiguosRef(sch1, sch2, ref) {
137: if (sch2 !== undefined && !equal(sch1, sch2))
^
138: throw ambiguos(ref);
139: }
(!) Illegal reassignment to import 'equal'
lib/runtime/equal.ts (3:0)
1: // https://github.com/ajv-validator/ajv/issues/889
2: import * as equal from "fast-deep-equal";
3: equal.code = 'require("ajv/dist/runtime/equal").default';
^
4: export default equal;
5: //# sourceMappingURL=equal.js.map
(!) Mixing named and default exports
https://rollupjs.org/guide/en/#outputexports
The following entry modules are using named and default exports together:
lib/2019.ts
lib/2019.ts
Consumers of your bundle will have to use chunk['default'] to access their default export, which may not be what you want. Use `output.exports: 'named'` to disable this warning
(!) Plugin typescript: @rollup/plugin-typescript: Rollup 'sourcemap' option must be set to generate source maps.
created ./bundle/ajv2019.bundle.js, ./bundle/ajv2019.min.js in 3.9s
./lib/2020.ts → ./bundle/ajv2020.bundle.js, ./bundle/ajv2020.min.js...
(!) Cannot call a namespace ('traverse')
lib/compile/resolve.ts (95:4)
93: const localRefs = {};
94: const schemaRefs = new Set();
95: traverse(schema, { allKeys: true }, (sch, jsonPtr, _, parentJsonPtr) => {
^
96: if (parentJsonPtr === undefined)
97: return;
(!) Cannot call a namespace ('equal')
lib/compile/resolve.ts (137:35)
135: return localRefs;
136: function checkAmbiguosRef(sch1, sch2, ref) {
137: if (sch2 !== undefined && !equal(sch1, sch2))
^
138: throw ambiguos(ref);
139: }
(!) Illegal reassignment to import 'equal'
lib/runtime/equal.ts (3:0)
1: // https://github.com/ajv-validator/ajv/issues/889
2: import * as equal from "fast-deep-equal";
3: equal.code = 'require("ajv/dist/runtime/equal").default';
^
4: export default equal;
5: //# sourceMappingURL=equal.js.map
(!) Mixing named and default exports
https://rollupjs.org/guide/en/#outputexports
The following entry modules are using named and default exports together:
lib/2020.ts
lib/2020.ts
Consumers of your bundle will have to use chunk['default'] to access their default export, which may not be what you want. Use `output.exports: 'named'` to disable this warning
(!) Plugin typescript: @rollup/plugin-typescript: Rollup 'sourcemap' option must be set to generate source maps.
created ./bundle/ajv2020.bundle.js, ./bundle/ajv2020.min.js in 3.5s
./lib/jtd.ts → ./bundle/ajvJTD.bundle.js, ./bundle/ajvJTD.min.js...
(!) Cannot call a namespace ('traverse')
lib/compile/resolve.ts (95:4)
93: const localRefs = {};
94: const schemaRefs = new Set();
95: traverse(schema, { allKeys: true }, (sch, jsonPtr, _, parentJsonPtr) => {
^
96: if (parentJsonPtr === undefined)
97: return;
(!) Cannot call a namespace ('equal')
lib/compile/resolve.ts (137:35)
135: return localRefs;
136: function checkAmbiguosRef(sch1, sch2, ref) {
137: if (sch2 !== undefined && !equal(sch1, sch2))
^
138: throw ambiguos(ref);
139: }
(!) Mixing named and default exports
https://rollupjs.org/guide/en/#outputexports
The following entry modules are using named and default exports together:
lib/jtd.ts
lib/jtd.ts
Consumers of your bundle will have to use chunk['default'] to access their default export, which may not be what you want. Use `output.exports: 'named'` to disable this warning
(!) Plugin typescript: @rollup/plugin-typescript: Rollup 'sourcemap' option must be set to generate source maps.
created ./bundle/ajvJTD.bundle.js, ./bundle/ajvJTD.min.js in 3.7s
Preparing browser tests:
spec/json-schema.spec.ts
TypeScript error: /home/coderextreme/ajv/spec/json_parse_tests.json(1059,21): Error TS1002: Unterminated string literal.
spec/errors.spec.ts
TypeScript error: /home/coderextreme/ajv/spec/json_parse_tests.json(1059,21): Error TS1002: Unterminated string literal.
spec/resolve.spec.ts
TypeScript error: /home/coderextreme/ajv/spec/json_parse_tests.json(1059,21): Error TS1002: Unterminated string literal.
spec/coercion.spec.ts
TypeScript error: /home/coderextreme/ajv/spec/json_parse_tests.json(1059,21): Error TS1002: Unterminated string literal.
spec/discriminator.spec.ts
TypeScript error: /home/coderextreme/ajv/spec/json_parse_tests.json(1059,21): Error TS1002: Unterminated string literal.
spec/dynamic-ref.spec.ts
TypeScript error: /home/coderextreme/ajv/spec/json_parse_tests.json(1059,21): Error TS1002: Unterminated string literal.
spec/extras.spec.ts
TypeScript error: /home/coderextreme/ajv/spec/json_parse_tests.json(1059,21): Error TS1002: Unterminated string literal.
spec/ajv.spec.ts
TypeScript error: /home/coderextreme/ajv/spec/json_parse_tests.json(1059,21): Error TS1002: Unterminated string literal.
spec/issues/768_passContext_recursive_ref.spec.ts
TypeScript error: /home/coderextreme/ajv/spec/json_parse_tests.json(1059,21): Error TS1002: Unterminated string literal.
spec/issues/204_options_schemas_data_together.spec.ts
TypeScript error: /home/coderextreme/ajv/spec/json_parse_tests.json(1059,21): Error TS1002: Unterminated string literal.
spec/issues/1625_evaluated_truthy_pattern_properties.spec.ts
TypeScript error: /home/coderextreme/ajv/spec/json_parse_tests.json(1059,21): Error TS1002: Unterminated string literal.
spec/issues/1344_non_root_recursive_ref_standalone.spec.ts
TypeScript error: /home/coderextreme/ajv/spec/json_parse_tests.json(1059,21): Error TS1002: Unterminated string literal.
spec/issues/240_mutual_recur_frags_common_ref.spec.ts
TypeScript error: /home/coderextreme/ajv/spec/json_parse_tests.json(1059,21): Error TS1002: Unterminated string literal.
spec/issues/50_refs_with_definitions.spec.ts
TypeScript error: /home/coderextreme/ajv/spec/json_parse_tests.json(1059,21): Error TS1002: Unterminated string literal.
spec/issues/259_validate_meta_against_itself.spec.ts
TypeScript error: /home/coderextreme/ajv/spec/json_parse_tests.json(1059,21): Error TS1002: Unterminated string literal.
spec/issues/743_removeAdditional_to_remove_proto.spec.ts
TypeScript error: /home/coderextreme/ajv/spec/json_parse_tests.json(1059,21): Error TS1002: Unterminated string literal.
spec/issues/273_error_schemaPath_refd_schema.spec.ts
TypeScript error: /home/coderextreme/ajv/spec/json_parse_tests.json(1059,21): Error TS1002: Unterminated string literal.
spec/issues/1001_addKeyword_and_schema_without_id.spec.ts
TypeScript error: /home/coderextreme/ajv/spec/json_parse_tests.json(1059,21): Error TS1002: Unterminated string literal.
spec/issues/815_id_updates_ref_base.spec.ts
TypeScript error: /home/coderextreme/ajv/spec/json_parse_tests.json(1059,21): Error TS1002: Unterminated string literal.
spec/issues/8_shared_refs.spec.ts
TypeScript error: /home/coderextreme/ajv/spec/json_parse_tests.json(1059,21): Error TS1002: Unterminated string literal.
spec/issues/521_wrong_warning_id_property.spec.ts
TypeScript error: /home/coderextreme/ajv/spec/json_parse_tests.json(1059,21): Error TS1002: Unterminated string literal.
spec/issues/1414_base_uri_change.spec.ts
TypeScript error: /home/coderextreme/ajv/spec/json_parse_tests.json(1059,21): Error TS1002: Unterminated string literal.
spec/issues/485_type_validation_priority.spec.ts
TypeScript error: /home/coderextreme/ajv/spec/json_parse_tests.json(1059,21): Error TS1002: Unterminated string literal.
spec/issues/955_removeAdditional_custom_keywords.spec.ts
TypeScript error: /home/coderextreme/ajv/spec/json_parse_tests.json(1059,21): Error TS1002: Unterminated string literal.
spec/issues/1515_evaluated_properties_nested_anyof.spec.ts
TypeScript error: /home/coderextreme/ajv/spec/json_parse_tests.json(1059,21): Error TS1002: Unterminated string literal.
spec/issues/1501_jtd_many_properties.spec.ts
TypeScript error: /home/coderextreme/ajv/spec/json_parse_tests.json(1059,21): Error TS1002: Unterminated string literal.
spec/issues/210_mutual_recur_frags.spec.ts
TypeScript error: /home/coderextreme/ajv/spec/json_parse_tests.json(1059,21): Error TS1002: Unterminated string literal.
spec/issues/342_uniqueItems_non-json_objects.spec.ts
TypeScript error: /home/coderextreme/ajv/spec/json_parse_tests.json(1059,21): Error TS1002: Unterminated string literal.
spec/issues/1539_add_keyword_name_to_validation_error.spec.ts
TypeScript error: /home/coderextreme/ajv/spec/json_parse_tests.json(1059,21): Error TS1002: Unterminated string literal.
spec/issues/182_nan_validation.spec.ts
TypeScript error: /home/coderextreme/ajv/spec/json_parse_tests.json(1059,21): Error TS1002: Unterminated string literal.
spec/issues/181_allErrors_custom_keyword_skipped.spec.ts
TypeScript error: /home/coderextreme/ajv/spec/json_parse_tests.json(1059,21): Error TS1002: Unterminated string literal.
spec/jtd-schema.spec.ts
TypeScript error: /home/coderextreme/ajv/spec/json_parse_tests.json(1059,21): Error TS1002: Unterminated string literal.
spec/javacript.spec.js
TypeScript error: /home/coderextreme/ajv/spec/json_parse_tests.json(1059,21): Error TS1002: Unterminated string literal.
spec/types/json-schema.spec.ts
TypeScript error: /home/coderextreme/ajv/spec/json_parse_tests.json(1059,21): Error TS1002: Unterminated string literal.
spec/types/async-validate.spec.ts
TypeScript error: /home/coderextreme/ajv/spec/json_parse_tests.json(1059,21): Error TS1002: Unterminated string literal.
spec/types/error-parameters.spec.ts
TypeScript error: /home/coderextreme/ajv/spec/json_parse_tests.json(1059,21): Error TS1002: Unterminated string literal.
spec/types/jtd-schema.spec.ts
TypeScript error: /home/coderextreme/ajv/spec/json_parse_tests.json(1059,21): Error TS1002: Unterminated string literal.
spec/boolean.spec.ts
TypeScript error: /home/coderextreme/ajv/spec/json_parse_tests.json(1059,21): Error TS1002: Unterminated string literal.
spec/jtd-timestamps.spec.ts
TypeScript error: /home/coderextreme/ajv/spec/json_parse_tests.json(1059,21): Error TS1002: Unterminated string literal.
spec/standalone.spec.ts
TypeScript error: /home/coderextreme/ajv/spec/json_parse_tests.json(1059,21): Error TS1002: Unterminated string literal.
spec/async.spec.ts
TypeScript error: /home/coderextreme/ajv/spec/json_parse_tests.json(1059,21): Error TS1002: Unterminated string literal.
spec/codegen.spec.ts
TypeScript error: /home/coderextreme/ajv/spec/json_parse_tests.json(1059,21): Error TS1002: Unterminated string literal.
spec/schema-tests.spec.ts
TypeScript error: /home/coderextreme/ajv/spec/json_parse_tests.json(1059,21): Error TS1002: Unterminated string literal.
spec/keyword.spec.ts
TypeScript error: /home/coderextreme/ajv/spec/json_parse_tests.json(1059,21): Error TS1002: Unterminated string literal.
spec/async_schemas.spec.ts
TypeScript error: /home/coderextreme/ajv/spec/json_parse_tests.json(1059,21): Error TS1002: Unterminated string literal.
spec/async_validate.spec.ts
TypeScript error: /home/coderextreme/ajv/spec/json_parse_tests.json(1059,21): Error TS1002: Unterminated string literal.
spec/security.spec.ts
TypeScript error: /home/coderextreme/ajv/spec/json_parse_tests.json(1059,21): Error TS1002: Unterminated string literal.
spec/options/options_reporting.spec.ts
TypeScript error: /home/coderextreme/ajv/spec/json_parse_tests.json(1059,21): Error TS1002: Unterminated string literal.
spec/options/strict.spec.ts
TypeScript error: /home/coderextreme/ajv/spec/json_parse_tests.json(1059,21): Error TS1002: Unterminated string literal.
spec/options/strictDefaults.spec.ts
TypeScript error: /home/coderextreme/ajv/spec/json_parse_tests.json(1059,21): Error TS1002: Unterminated string literal.
spec/options/meta_validateSchema.spec.ts
TypeScript error: /home/coderextreme/ajv/spec/json_parse_tests.json(1059,21): Error TS1002: Unterminated string literal.
spec/options/unknownFormats.spec.ts
TypeScript error: /home/coderextreme/ajv/spec/json_parse_tests.json(1059,21): Error TS1002: Unterminated string literal.
spec/options/nullable.spec.ts
TypeScript error: /home/coderextreme/ajv/spec/json_parse_tests.json(1059,21): Error TS1002: Unterminated string literal.
spec/options/options_add_schemas.spec.ts
TypeScript error: /home/coderextreme/ajv/spec/json_parse_tests.json(1059,21): Error TS1002: Unterminated string literal.
spec/options/comment.spec.ts
TypeScript error: /home/coderextreme/ajv/spec/json_parse_tests.json(1059,21): Error TS1002: Unterminated string literal.
spec/options/schemaId.spec.ts
TypeScript error: /home/coderextreme/ajv/spec/json_parse_tests.json(1059,21): Error TS1002: Unterminated string literal.
spec/options/removeAdditional.spec.ts
TypeScript error: /home/coderextreme/ajv/spec/json_parse_tests.json(1059,21): Error TS1002: Unterminated string literal.
spec/options/options_code.spec.ts
TypeScript error: /home/coderextreme/ajv/spec/json_parse_tests.json(1059,21): Error TS1002: Unterminated string literal.
spec/options/unicodeRegExp.spec.ts
TypeScript error: /home/coderextreme/ajv/spec/json_parse_tests.json(1059,21): Error TS1002: Unterminated string literal.
spec/options/strictKeywords.spec.ts
TypeScript error: /home/coderextreme/ajv/spec/json_parse_tests.json(1059,21): Error TS1002: Unterminated string literal.
spec/options/int32range.spec.ts
TypeScript error: /home/coderextreme/ajv/spec/json_parse_tests.json(1059,21): Error TS1002: Unterminated string literal.
spec/options/options_refs.spec.ts
TypeScript error: /home/coderextreme/ajv/spec/json_parse_tests.json(1059,21): Error TS1002: Unterminated string literal.
spec/options/useDefaults.spec.ts
TypeScript error: /home/coderextreme/ajv/spec/json_parse_tests.json(1059,21): Error TS1002: Unterminated string literal.
spec/options/options_validation.spec.ts
TypeScript error: /home/coderextreme/ajv/spec/json_parse_tests.json(1059,21): Error TS1002: Unterminated string literal.
spec/options/strictNumbers.spec.ts
TypeScript error: /home/coderextreme/ajv/spec/json_parse_tests.json(1059,21): Error TS1002: Unterminated string literal.
spec/options/ownProperties.spec.ts
TypeScript error: /home/coderextreme/ajv/spec/json_parse_tests.json(1059,21): Error TS1002: Unterminated string literal.
I went ahead anyway, despite failing tests. Here's what I got in Chromium:
Uncaught ReferenceError: module is not defined
at ajv2020.bundle.js:7211
at ajv2020.bundle.js:4
at ajv2020.bundle.js:5
Firefox:
Uncaught ReferenceError: module is not defined
<anonymous> https://localhost:3000/node/ajv2020.bundle.js:7211
<anonymous> https://localhost:3000/node/ajv2020.bundle.js:4
<anonymous> https://localhost:3000/node/ajv2020.bundle.js:5
Here's how I include the bundle:
<script type="text/javascript">exports = {};</script>
<script src="../node/ajv2020.bundle.js"></script>
I get this error on the console:
Uncaught TypeError: traverse$1 is not a function
getSchemaRefs https://localhost:3000/src/main/node/ajv2020.bundle.js:3494
_addSchema https://localhost:3000/src/main/node/ajv2020.bundle.js:4741
addSchema https://localhost:3000/src/main/node/ajv2020.bundle.js:4525
addMetaSchema https://localhost:3000/src/main/node/ajv2020.bundle.js:4533
addMetaSchema2020 https://localhost:3000/src/main/node/ajv2020.bundle.js:7175
addMetaSchema2020 https://localhost:3000/src/main/node/ajv2020.bundle.js:7175
_addDefaultMetaSchema https://localhost:3000/src/main/node/ajv2020.bundle.js:7203
Ajv https://localhost:3000/src/main/node/ajv2020.bundle.js:4405
Ajv2020 https://localhost:3000/src/main/node/ajv2020.bundle.js:7185
<anonymous> https://localhost:3000/src/main/node/loadValidate.js:2
I have made these changes:
$ diff ~/ajv/bundle/ajv2020.bundle.js ajv2020.bundle.js
7211c7211
< module.exports = exports = Ajv2020;
---
> module.exports = exports = window.ajv2020 = Ajv2020;
This is how I use the bundle, partially taken from: https://ajv.js.org/guide/environments.html#browsers
var Ajv = window.ajv2020;
var ajv = new Ajv({allErrors:true, verbose:true, strict:false});
8.7.1 has 2020 bundle - can you please check if it works once it gets to cdnjs? It can be a day, probably.
I'm not sure if my email made it here or not. I use:
To get bundle. I get that Ajv2020 is not a constructor. The error is thrown in a file like:
var ajv = new Ajv2020({allErrors:true, verbose:true});
I'm not able to remove require without making Ajv undefined
Found this: https://ajv.js.org/guide/environments.html#browsers
I don't think it's relevant tbh... I think the way Ajv is exported for node.js to avoid using .default
prevents the rollup UMD bundle from correctly exporting Ajv, as exports is overridden. Need to think what the solution is, but it's clear now what's not working.
I’m glad you have a good perspective on it. I think I tried to create a global exports object before loading Ajv in the browser.
While getting 2020-12 working in the command environment was relatively easy, the web offers a higher learning curve, perhaps through some kind of bundling with my app with browserify or perhaps webpack— I’ve only tried browserify.
I have seen people apparently using Ajv in jsfiddle, but I was on my phone, so I couldn’t see code.
The good news is, I haven’t changed most of my html files which rely on Ajv6, and my production site still works well. I’m crossing fingers for now.
I’m also having issues with my schema with Java tools, but afaik, no one is using that code yet.
On a more positive note, I think Ajv8 screams on the command line, congratulations.
The main thing I tried to do working with Ajv in the browser was assign the window.Ajv2020 object. That may help you.
John
On Tue, Nov 9, 2021 at 4:12 PM Evgeny Poberezkin @.***> wrote:
Found this: https://ajv.js.org/guide/environments.html#browsers
I don't think it's relevant tbh... I think the way Ajv is exported for node.js to avoid using .default prevents the rollup UMD bundle from correctly exporting Ajv, as exports is overridden. Need to think what the solution is, but it's clear now what's not working.
— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/ajv-validator/ajv/issues/1765#issuecomment-964593186, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAFMJ57ETIZ3REBBY7RFALTULGMFLANCNFSM5EBR5MIA .
Compared to stuff running under ant or python, Ajv8 really shines for performance!
We have nearly 4000+ examples we regression test. Fast runs means we can do more of them!
John
On Tue, Nov 9, 2021 at 4:38 PM John Carlson @.***> wrote:
I’m glad you have a good perspective on it. I think I tried to create a global exports object before loading Ajv in the browser.
While getting 2020-12 working in the command environment was relatively easy, the web offers a higher learning curve, perhaps through some kind of bundling with my app with browserify or perhaps webpack— I’ve only tried browserify.
I have seen people apparently using Ajv in jsfiddle, but I was on my phone, so I couldn’t see code.
The good news is, I haven’t changed most of my html files which rely on Ajv6, and my production site still works well. I’m crossing fingers for now.
I’m also having issues with my schema with Java tools, but afaik, no one is using that code yet.
On a more positive note, I think Ajv8 screams on the command line, congratulations.
The main thing I tried to do working with Ajv in the browser was assign the window.Ajv2020 object. That may help you.
John
On Tue, Nov 9, 2021 at 4:12 PM Evgeny Poberezkin @.***> wrote:
Found this: https://ajv.js.org/guide/environments.html#browsers
I don't think it's relevant tbh... I think the way Ajv is exported for node.js to avoid using .default prevents the rollup UMD bundle from correctly exporting Ajv, as exports is overridden. Need to think what the solution is, but it's clear now what's not working.
— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/ajv-validator/ajv/issues/1765#issuecomment-964593186, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAFMJ57ETIZ3REBBY7RFALTULGMFLANCNFSM5EBR5MIA .
I also noticed the better error handling with nested oneOfs. That will help my users tremendously!
Thank you for your diligent efforts!
John
On Tue, Nov 9, 2021 at 4:12 PM Evgeny Poberezkin @.***> wrote:
Found this: https://ajv.js.org/guide/environments.html#browsers
I don't think it's relevant tbh... I think the way Ajv is exported for node.js to avoid using .default prevents the rollup UMD bundle from correctly exporting Ajv, as exports is overridden. Need to think what the solution is, but it's clear now what's not working.
— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/ajv-validator/ajv/issues/1765#issuecomment-964593186, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAFMJ57ETIZ3REBBY7RFALTULGMFLANCNFSM5EBR5MIA .
No worries, but I still can't make bundles work btw - it's been ages since I used the bundles (if I ever used them 😅) - I always just bundle Ajv with the app, as I guess most people do, so the issue was unreported for so long...
PR #1812 fixes export problem I think, but it might be breaking all the non-default node.js exports - although maybe not, as module.exports === exports in node, but in rollup exports is the name of the function passed to the variable... I will test it.
But regardless of that, rollup shows warnings during the build (it did show them before too, it's not related to this change:
./lib/2020.ts → ./bundle/ajv2020.bundle.js, ./bundle/ajv2020.min.js...
(!) Cannot call a namespace ('traverse')
lib/compile/resolve.ts (95:4)
93: const localRefs = {};
94: const schemaRefs = new Set();
95: traverse(schema, { allKeys: true }, (sch, jsonPtr, _, parentJsonPtr) => {
^
96: if (parentJsonPtr === undefined)
97: return;
(!) Cannot call a namespace ('equal')
lib/compile/resolve.ts (137:35)
135: return localRefs;
136: function checkAmbiguosRef(sch1, sch2, ref) {
137: if (sch2 !== undefined && !equal(sch1, sch2))
^
138: throw ambiguos(ref);
139: }
(!) Illegal reassignment to import 'equal'
lib/runtime/equal.ts (3:0)
1: // https://github.com/ajv-validator/ajv/issues/889
2: import * as equal from "fast-deep-equal";
3: equal.code = 'require("ajv/dist/runtime/equal").default';
^
4: export default equal;
5: //# sourceMappingURL=equal.js.map
(!) Mixing named and default exports
https://rollupjs.org/guide/en/#outputexports
The following entry modules are using named and default exports together:
lib/2020.ts
lib/2020.ts
Consumers of your bundle will have to use chunk['default'] to access their default export, which may not be what you want. Use `output.exports: 'named'` to disable this warning
(!) Plugin typescript: @rollup/plugin-typescript: Rollup 'sourcemap' option must be set to generate source maps.
created ./bundle/ajv2020.bundle.js, ./bundle/ajv2020.min.js in 4.5s
And then when I use Ajv in the browser with this code:
const Ajv = window.ajv7.default;
console.log(Ajv);
const ajv = new Ajv();
console.log(ajv.validate({type: "number"}, 1));
console.log(ajv.validate({type: "number"}, "a"));
it fails on ajv construction with this message:
class Ajv2020 extends Ajv {
constructor(opts = {}) {
super({
...opts,
dynamicRef: true,
next: true,
unevaluated: true,
…
ajv2020.bundle.js:3494 Uncaught TypeError: traverse$1 is not a function
at Ajv2020.getSchemaRefs (ajv2020.bundle.js:3494)
at Ajv2020._addSchema (ajv2020.bundle.js:4741)
at Ajv2020.addSchema (ajv2020.bundle.js:4525)
at Ajv2020.addMetaSchema (ajv2020.bundle.js:4533)
at ajv2020.bundle.js:7183
at Array.forEach (<anonymous>)
at Ajv2020.addMetaSchema2020 (ajv2020.bundle.js:7183)
at Ajv2020._addDefaultMetaSchema (ajv2020.bundle.js:7211)
at new Ajv (ajv2020.bundle.js:4405)
at new Ajv2020 (ajv2020.bundle.js:7193)
and it did complain about traverse during the build.
@realityking - you've contributed rollup bundling - how do you use it in the browser (if you do of course:)?
@coderextreme - your help diagnosing/resolving it is very much appreciated, but I seem stuck here - any ideas? Short of changing bundling mechanism completely...
Ok, so simply reverting to good old browserify (reverting this commit) fixes everything.
Bundling in PR #1812 should be done with npm run browserify
(I didn't remove rollup yet) and the bundles just work, without even .default
:
const Ajv = window.ajv2020;
const ajv = new Ajv();
console.log(ajv.validate({type: "number"}, 1));
console.log(ajv.validate({type: "number"}, "a"));
So without any plugins, browserify that wasn't updated for a year, makes the bundle that works in a fraction of rollup's time. Browserify bundles are indeed larger as @realityking wrote in #1533, but unlike rollup bundles they actually work and seem more flexible to various exports.
@coderextreme - it would be great if you could test the bundle that the new (old) bundling script produces.
Unless there is any idea how to fix rollup bundles without breaking node.js exports I am going to revert to browserify in a few days in 8.7.2
I don’t use ajv in the browser but I’d be happy to take a look after Nov 20th (I’m on my honeymoon right now)
I will check out main branch of ajv and work on creating a browserify bundle for Ajv2020 to deploy with my app. That seems like the right approach.
If I need to merge a PR (fork, etc.) let me know. It appears like I need to merge PR #1812 from the below.
I’m expecting to work on this at 9 CST.
John
On Wed, Nov 10, 2021 at 2:10 AM Evgeny Poberezkin @.***> wrote:
Ok, so simply reverting to good old browserify (reverting this commit https://github.com/ajv-validator/ajv/commit/b7809449d9f829c061880a0d48a8dc3779d6ef66) fixes everything.
Bundling in PR #1812 https://github.com/ajv-validator/ajv/pull/1812 should be done with npm run browserify (I didn't remove rollup yet) and the bundles just work, without even .default:
const Ajv = window.ajv2020;const ajv = new Ajv();console.log(ajv.validate({type: "number"}, 1));console.log(ajv.validate({type: "number"}, "a"));
So without any plugins, browserify that wasn't updated for a year, makes the bundle that works in a fraction of rollup's time. Browserify bundles are indeed larger as @realityking https://github.com/realityking wrote in #1533 https://github.com/ajv-validator/ajv/pull/1533, but unlike rollup bundles they actually work and seem more flexible to various exports.
@coderextreme https://github.com/coderextreme - it would be great if you could test the bundle that the new (old) bundling script produces.
Unless there is any idea how to fix rollup bundles without breaking node.js exports I am going to revert to browserify in a few days in 8.7.2
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/ajv-validator/ajv/issues/1765#issuecomment-964878461, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAFMJ5562KHBEGQ24X4R2BTULISIHANCNFSM5EBR5MIA .
I made good progress using Ajv from the web, but I'm now stuck at uri formats:
Uncaught Error: unknown format "uri" ignored in schema at path "#/properties/%40xsd%3AnoNamespaceSchemaLocation" unknownFormat https://localhost:3000/node/ajv2020.bundle.js:5295 validateFormat https://localhost:3000/node/ajv2020.bundle.js:5282 code https://localhost:3000/node/ajv2020.bundle.js:5254 keywordCode https://localhost:3000/node/ajv2020.bundle.js:2525 iterateKeywords https://localhost:3000/node/ajv2020.bundle.js:2293 code https://localhost:3000/node/ajv2020.bundle.js:588 block https://localhost:3000/node/ajv2020.bundle.js:717 iterateKeywords https://localhost:3000/node/ajv2020.bundle.js:2290 groupKeywords https://localhost:3000/node/ajv2020.bundle.js:2271 schemaKeywords https://localhost:3000/node/ajv2020.bundle.js:2263 code https://localhost:3000/node/ajv2020.bundle.js:588 block https://localhost:3000/node/ajv2020.bundle.js:717 schemaKeywords https://localhost:3000/node/ajv2020.bundle.js:2261 typeAndKeywords https://localhost:3000/node/ajv2020.bundle.js:2199 subSchemaObjCode https://localhost:3000/node/ajv2020.bundle.js:2186 subschemaCode https://localhost:3000/node/ajv2020.bundle.js:2162 subschema https://localhost:3000/node/ajv2020.bundle.js:2499 applyPropertySchema https://localhost:3000/node/ajv2020.bundle.js:4641 code https://localhost:3000/node/ajv2020.bundle.js:4629 keywordCode https://localhost:3000/node/ajv2020.bundle.js:2525 iterateKeywords https://localhost:3000/node/ajv2020.bundle.js:2293 code https://localhost:3000/node/ajv2020.bundle.js:588 block https://localhost:3000/node/ajv2020.bundle.js:717 iterateKeywords https://localhost:3000/node/ajv2020.bundle.js:2290 groupKeywords https://localhost:3000/node/ajv2020.bundle.js:2271 schemaKeywords https://localhost:3000/node/ajv2020.bundle.js:2263 code https://localhost:3000/node/ajv2020.bundle.js:588 block https://localhost:3000/node/ajv2020.bundle.js:717 schemaKeywords https://localhost:3000/node/ajv2020.bundle.js:2261 typeAndKeywords https://localhost:3000/node/ajv2020.bundle.js:2199 topSchemaObjCode https://localhost:3000/node/ajv2020.bundle.js:2141 code https://localhost:3000/node/ajv2020.bundle.js:588 validateFunction https://localhost:3000/node/ajv2020.bundle.js:2108 code https://localhost:3000/node/ajv2020.bundle.js:588 func https://localhost:3000/node/ajv2020.bundle.js:736 validateFunction https://localhost:3000/node/ajv2020.bundle.js:2108 topSchemaObjCode https://localhost:3000/node/ajv2020.bundle.js:2133 validateFunctionCode https://localhost:3000/node/ajv2020.bundle.js:2092 compileSchema https://localhost:3000/node/ajv2020.bundle.js:1196 inlineOrCompile https://localhost:3000/node/ajv2020.bundle.js:1264 resolveRef https://localhost:3000/node/ajv2020.bundle.js:1258 code https://localhost:3000/node/ajv2020.bundle.js:4879 keywordCode https://localhost:3000/node/ajv2020.bundle.js:2525 schemaKeywords https://localhost:3000/node/ajv2020.bundle.js:2256 code https://localhost:3000/node/ajv2020.bundle.js:588 block https://localhost:3000/node/ajv2020.bundle.js:717 schemaKeywords https://localhost:3000/node/ajv2020.bundle.js:2256 typeAndKeywords https://localhost:3000/node/ajv2020.bundle.js:2199 subSchemaObjCode https://localhost:3000/node/ajv2020.bundle.js:2186 subschemaCode https://localhost:3000/node/ajv2020.bundle.js:2162 subschema https://localhost:3000/node/ajv2020.bundle.js:2499 applyPropertySchema https://localhost:3000/node/ajv2020.bundle.js:4641 code https://localhost:3000/node/ajv2020.bundle.js:4629 keywordCode https://localhost:3000/node/ajv2020.bundle.js:2525 iterateKeywords https://localhost:3000/node/ajv2020.bundle.js:2293 code https://localhost:3000/node/ajv2020.bundle.js:588 block https://localhost:3000/node/ajv2020.bundle.js:717 iterateKeywords https://localhost:3000/node/ajv2020.bundle.js:2290 groupKeywords https://localhost:3000/node/ajv2020.bundle.js:2271 schemaKeywords https://localhost:3000/node/ajv2020.bundle.js:2263 code https://localhost:3000/node/ajv2020.bundle.js:588 block https://localhost:3000/node/ajv2020.bundle.js:717 schemaKeywords https://localhost:3000/node/ajv2020.bundle.js:2261 typeAndKeywords https://localhost:3000/node/ajv2020.bundle.js:2199 topSchemaObjCode https://localhost:3000/node/ajv2020.bundle.js:2141 code https://localhost:3000/node/ajv2020.bundle.js:588 validateFunction https://localhost:3000/node/ajv2020.bundle.js:2108 code https://localhost:3000/node/ajv2020.bundle.js:588 func https://localhost:3000/node/ajv2020.bundle.js:736 validateFunction https://localhost:3000/node/ajv2020.bundle.js:2108 topSchemaObjCode https://localhost:3000/node/ajv2020.bundle.js:2133 validateFunctionCode https://localhost:3000/node/ajv2020.bundle.js:2092 compileSchema https://localhost:3000/node/ajv2020.bundle.js:1196 _compileSchemaEnv https://localhost:3000/node/ajv2020.bundle.js:3255 compile https://localhost:3000/node/ajv2020.bundle.js:2942 addSchema https://localhost:3000/node/loadValidate.js:77 loadSchema https://localhost:3000/node/loadValidate.js:98 jQuery 8 loadSchema https://localhost:3000/node/loadValidate.js:97 jQuery 15
I was trying:
addFormats(ajv, [ "uri", "uri-reference" ])
but that doesn't work, fairly obviously.
You either need to bundle ajv-formats with your code, which I think is better (with whatever bundling mechanism you use for your code - webpack, rollup, browserify - I can’t advise on what’s better for you), as it’s quite small, or create a separate browser bundle (again, with either of those tools). I’m not planning to create/maintain the browser bundle for ajv-formats.
This solved the problem with add uri and uri-reference to formats. I don't quite know how i found it. It's quite prized at this point, maybe!
addFormats(ajv, {mode: "full", formats: ["uri-reference", "uri"], keywords: true});
I think browserify works best with node.js code. I've tried the others...too complex!
I've been experimenting a bit with this:
#!/bin/bash
find node_modules/ajv node_modules/ajv-formats node_modules/ajv-i18n -name "*.js" | sort | egrep '2020.js|2019.js|ajv.js|index.js' | xargs browserify > src/main/node/ajvAll.js
What I've done is edit the ajvAll.js script to add "window." to export things that I want to use in my web browser code. I'll probably switch that to an "let ajvAll = {}" object and store what I want in there. What would be good is to create a node.js module which only requires what you need, Ajv2020, addFormats, etc. The run that module through browserify to include dependencies. Don't forget to add Ajv2020 and addFormats to your module.exports varaible.
I would like to use Ajv in a web page. For that purpose, I need to lead AJV as a standalone JavaScript file.
I have tried to follow the instructions here and to load a script from the CDN: https://cdnjs.com/libraries/ajv/8.6.1 but it didn't work.
What script exactly should I load from the CDN?
ajv.2019.min.js
orajv.2019.bundle.js
?I am sorry if I missed something...