Skip to content

Commit

Permalink
test: more browser friendly tests, rename draft-07 browser bundle and…
Browse files Browse the repository at this point in the history
… globals in both browser bundles
  • Loading branch information
epoberezkin committed Dec 13, 2020
1 parent 5c45d2e commit 2bcaadd
Show file tree
Hide file tree
Showing 25 changed files with 77 additions and 48 deletions.
28 changes: 23 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -282,19 +282,37 @@ See [Content Security Policy](./docs/security.md#content-security-policy) to dec

Whether you use Ajv or compiled schemas, it is recommended that you bundle them together with your code.

If you need to use Ajv in several bundles you can create a separate UMD bundle using `npm run bundle` script (thanks to [siddo420](https://github.com/siddo420)).
If you need to use Ajv in several bundles you can create a separate UMD bundles using `npm run bundle` script.

Then you need to load Ajv in the browser:
Then you need to load Ajv with support of JSON Schema draft-07 in the browser:

```html
<script src="ajv.min.js"></script>
<script src="bundle/ajv7.min.js"></script>
<script>
;(function () {
const Ajv = window.ajv7.default
const ajv = new Ajv()
})()
</script>
```

This bundle can be used with different module systems; it creates global `Ajv` if no module system is found.
or to load the bundle that supports JSONSchema draft-2019-09:

```html
<script src="bundle/ajv2019.min.js"></script>
<script>
;(function () {
const Ajv = window.ajv2019.default
const ajv = new Ajv()
})()
</script>
```

This bundle can be used with different module systems; it creates global `ajv` (or `ajv2019`) if no module system is found.

The browser bundle is available on [cdnjs](https://cdnjs.com/libraries/ajv).

**Please note**: some frameworks, e.g. Dojo, may redefine global require in such way that is not compatible with CommonJS module format. In such case Ajv bundle has to be loaded before the framework and then you can use global Ajv (see issue [#234](https://github.com/ajv-validator/ajv/issues/234)).
**Please note**: some frameworks, e.g. Dojo, may redefine global require in a way that is not compatible with CommonJS module format. In this case Ajv bundle has to be loaded before the framework and then you can use global `ajv` (see issue [#234](https://github.com/ajv-validator/ajv/issues/234)).

## Command line interface

Expand Down
7 changes: 6 additions & 1 deletion karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ module.exports = function (config) {
frameworks: ["mocha"],

// list of files / patterns to load in the browser
files: ["bundle/ajv.min.js", "node_modules/chai/chai.js", ".browser/*.spec.js"],
files: [
"bundle/ajv7.min.js",
"bundle/ajv2019.min.js",
"node_modules/chai/chai.js",
".browser/*.spec.js",
],

// test results reporter to use
// possible values: 'dots', 'progress'
Expand Down
2 changes: 1 addition & 1 deletion lib/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ export default class Ajv {
): boolean | Promise<T> {
let v: AnyValidateFunction | undefined
if (typeof schemaKeyRef == "string") {
v = this.getSchema(schemaKeyRef)
v = this.getSchema<T>(schemaKeyRef)
if (!v) throw new Error(`no schema with key or ref "${schemaKeyRef}"`)
} else {
v = this.compile<T>(schemaKeyRef)
Expand Down
2 changes: 1 addition & 1 deletion lib/standalone/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type AjvCore from "../core"
import type {AnyValidateFunction, SourceCode} from "../types"
import type {SchemaEnv} from "../compile"
import {ScopeValueSets, ValueScopeName, varKinds} from "../compile/codegen/scope"
import {_, _Code, Code, getProperty} from "../compile/codegen/code"
import {SchemaEnv} from "../compile"

export default function standaloneCode(
ajv: AjvCore,
Expand Down
2 changes: 1 addition & 1 deletion lib/standalone/instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export default class AjvPack {
errors?: ErrorObject[] | null // errors from the last validation
constructor(readonly ajv: Ajv) {}

validate<T = unknown>(schemaKeyRef: AnySchema | string, data: unknown): data is T | Promise<T> {
validate(schemaKeyRef: AnySchema | string, data: unknown): boolean | Promise<unknown> {
return Ajv.prototype.validate.call(this, schemaKeyRef, data)
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"test-codegen": "nyc cross-env TS_NODE_PROJECT=spec/tsconfig.json mocha -r ts-node/register 'spec/codegen.spec.ts' -R spec",
"test-debug": "npm run test-spec -- --inspect-brk",
"test-cov": "nyc npm run test-spec",
"bundle": "rm -rf bundle && node ./scripts/bundle.js ajv ajv Ajv && node ./scripts/bundle.js 2019 ajv2019 Ajv2019",
"bundle": "rm -rf bundle && node ./scripts/bundle.js ajv ajv7 ajv7 && node ./scripts/bundle.js 2019 ajv2019 ajv2019",
"build": "rm -rf dist && tsc && cp -r lib/refs dist && rm dist/refs/json-schema-2019-09/index.ts",
"json-tests": "rm -rf spec/_json/*.js && node scripts/jsontests",
"test-karma": "karma start",
Expand Down
2 changes: 1 addition & 1 deletion scripts/prepare-tests
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ echo Preparing browser tests:

find spec -type f -name '*.spec.*s' | \
xargs -I {} sh -c \
'export f="{}"; echo $f; browserify $f -p [ tsify -p ./spec/tsconfig.json ] -x ajv -u buffer -o $(echo $f | sed -e "s/spec/.browser/" | sed -e "s/.spec.ts/.spec.js/");'
'export f="{}"; echo $f; ./node_modules/.bin/browserify $f -p [ tsify -p ./spec/tsconfig.json ] -x ajv -u buffer -o $(echo $f | sed -e "s/spec/.browser/" | sed -e "s/.spec.ts/.spec.js/");'
2 changes: 1 addition & 1 deletion spec/ajv.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type Ajv from ".."
import type {KeywordCxt, SchemaObject} from ".."
import _Ajv from "./ajv"
import {_} from ".."
import {_} from "../dist/compile/codegen/code"
import assert = require("assert")
import chai from "./chai"
const should = chai.should()
Expand Down
5 changes: 2 additions & 3 deletions spec/ajv.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type Ajv from "../dist/core"

const AjvClass: typeof Ajv = typeof window == "object" ? (window as any).Ajv : require("" + "..")
const m = typeof window == "object" ? (window as any).ajv7 : require("" + "..")
const AjvClass: typeof Ajv = m.default

export default AjvClass

module.exports = AjvClass
6 changes: 2 additions & 4 deletions spec/ajv2019.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import type Ajv2019 from "../dist/2019"

const AjvClass: typeof Ajv2019 =
typeof window == "object" ? (window as any).Ajv2019 : require("" + "../dist/2019")
const m = typeof window == "object" ? (window as any).ajv2019 : require("" + "../dist/2019")
const AjvClass: typeof Ajv2019 = m.default

export default AjvClass

module.exports = AjvClass
11 changes: 0 additions & 11 deletions spec/ajv_instances.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type AjvCore from "../dist/core"
import type {Options} from ".."
import AjvPack from "../dist/standalone/instance"

export default function getAjvInstances(
_Ajv: typeof AjvCore,
Expand All @@ -22,13 +21,3 @@ export default function getAjvInstances(
return [new _Ajv(useOpts)]
}
}

export function withStandalone(instances: AjvCore[]): (AjvCore | AjvPack)[] {
return [...(instances as (AjvCore | AjvPack)[]), ...instances.map((ajv) => new AjvPack(ajv))]
}

export function getStandalone(_Ajv: typeof AjvCore, opts: Options = {}): AjvPack {
opts.code ||= {}
opts.code.source = true
return new AjvPack(new _Ajv(opts))
}
13 changes: 13 additions & 0 deletions spec/ajv_standalone.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import type AjvCore from "../dist/core"
import type {Options} from ".."
import AjvPack from "../dist/standalone/instance"

export function withStandalone(instances: AjvCore[]): (AjvCore | AjvPack)[] {
return [...(instances as (AjvCore | AjvPack)[]), ...instances.map((ajv) => new AjvPack(ajv))]
}

export function getStandalone(_Ajv: typeof AjvCore, opts: Options = {}): AjvPack {
opts.code ||= {}
opts.code.source = true
return new AjvPack(new _Ajv(opts))
}
3 changes: 2 additions & 1 deletion spec/async_schemas.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import jsonSchemaTest = require("json-schema-test")
import {afterError} from "./after_test"
import type Ajv from ".."
import _Ajv from "./ajv"
import chai from "./chai"

const instances = getAjvAsyncInstances({$data: true})

Expand All @@ -14,7 +15,7 @@ jsonSchemaTest(instances, {
suites: {"async schemas": require("./_json/async")},
async: true,
asyncValid: "data",
assert: require("./chai").assert,
assert: chai.assert,
afterError,
// afterEach: after.each,
cwd: __dirname,
Expand Down
5 changes: 2 additions & 3 deletions spec/chai.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import "chai"
import type {ChaiStatic} from "./chai_type"

const chai: Chai.ChaiStatic =
typeof window == "object" ? (window as any).chai : require("" + "chai")
const chai: ChaiStatic = typeof window == "object" ? (window as any).chai : require("" + "chai")

export default chai
2 changes: 2 additions & 0 deletions spec/chai_type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import "chai"
export type ChaiStatic = Chai.ChaiStatic
7 changes: 4 additions & 3 deletions spec/extras.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import getAjvAllInstances from "./ajv_all_instances"
import {withStandalone} from "./ajv_instances"
import {_} from ".."
import {withStandalone} from "./ajv_standalone"
import {_} from "../dist/compile/codegen/code"
import jsonSchemaTest = require("json-schema-test")
import options from "./ajv_options"
import {afterError, afterEach} from "./after_test"
import chai from "./chai"

const instances = getAjvAllInstances(options, {
$data: true,
Expand All @@ -21,7 +22,7 @@ jsonSchemaTest(withStandalone(instances), {
description:
"Extra keywords schemas tests of " + instances.length + " ajv instances with different options",
suites: {extras: require("./_json/extras")},
assert: require("./chai").assert,
assert: chai.assert,
afterError,
afterEach,
cwd: __dirname,
Expand Down
2 changes: 1 addition & 1 deletion spec/issues/210_mutual_recur_frags.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type AjvCore from "../../dist/core"
import type AjvPack from "../../dist/standalone/instance"
import {getStandalone} from "../ajv_instances"
import {getStandalone} from "../ajv_standalone"
import _Ajv from "../ajv"
import chai from "../chai"
chai.should()
Expand Down
2 changes: 1 addition & 1 deletion spec/issues/240_mutual_recur_frags_common_ref.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type AjvCore from "../../dist/core"
import type AjvPack from "../../dist/standalone/instance"
import {getStandalone} from "../ajv_instances"
import {getStandalone} from "../ajv_standalone"
import _Ajv from "../ajv"
import chai from "../chai"
chai.should()
Expand Down
2 changes: 1 addition & 1 deletion spec/issues/50_refs_with_definitions.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type AjvCore from "../../dist/core"
import type AjvPack from "../../dist/standalone/instance"
import {getStandalone} from "../ajv_instances"
import {getStandalone} from "../ajv_standalone"
import _Ajv from "../ajv"
import chai from "../chai"
const should = chai.should()
Expand Down
2 changes: 1 addition & 1 deletion spec/issues/8_shared_refs.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type AjvCore from "../../dist/core"
import type AjvPack from "../../dist/standalone/instance"
import _Ajv from "../ajv"
import {getStandalone} from "../ajv_instances"
import {getStandalone} from "../ajv_standalone"
import chai from "../chai"
chai.should()

Expand Down
4 changes: 2 additions & 2 deletions spec/javacript.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const Ajv = require("..").default
const Ajv2019 = require("../dist/2019").default
const Ajv = require("./ajv").default
const Ajv2019 = require("./ajv2019").default
const assert = require("assert")

describe("using Ajv with javascript", () => {
Expand Down
3 changes: 2 additions & 1 deletion spec/json-schema.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import _Ajv from "./ajv"
import _Ajv2019 from "./ajv2019"
import getAjvInstances, {withStandalone} from "./ajv_instances"
import getAjvInstances from "./ajv_instances"
import {withStandalone} from "./ajv_standalone"
import jsonSchemaTest = require("json-schema-test")
import options from "./ajv_options"
import {afterError, afterEach} from "./after_test"
Expand Down
2 changes: 1 addition & 1 deletion spec/keyword.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type {ErrorObject, SchemaObject, SchemaValidateFunction} from "../lib/types"
import type AjvCore from "../dist/core"
// currently most tests include compiled code, if any code re-compiled locally, instanceof would fail
import {_, nil} from ".."
import {_, nil} from "../dist/compile/codegen/code"
import getAjvAllInstances from "./ajv_all_instances"
import _Ajv from "./ajv"
import assert = require("assert")
Expand Down
3 changes: 2 additions & 1 deletion spec/schema-tests.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type AjvCore from "../dist/core"
import _Ajv from "./ajv"
import getAjvInstances, {withStandalone} from "./ajv_instances"
import getAjvInstances from "./ajv_instances"
import {withStandalone} from "./ajv_standalone"
import jsonSchemaTest = require("json-schema-test")
import options from "./ajv_options"
import {afterError, afterEach} from "./after_test"
Expand Down
6 changes: 4 additions & 2 deletions spec/security.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import _Ajv from "./ajv"
import getAjvInstances, {withStandalone} from "./ajv_instances"
import getAjvInstances from "./ajv_instances"
import {withStandalone} from "./ajv_standalone"
import jsonSchemaTest = require("json-schema-test")
import options from "./ajv_options"
import {afterError, afterEach} from "./after_test"
import chai from "./chai"

const instances = getAjvInstances(_Ajv, options, {
schemas: [require("../dist/refs/json-schema-secure.json")],
Expand All @@ -15,7 +17,7 @@ jsonSchemaTest(withStandalone(instances), {
description:
"Secure schemas tests of " + instances.length + " ajv instances with different options",
suites: {security: require("./_json/security")},
assert: require("./chai").assert,
assert: chai.assert,
afterError,
afterEach,
cwd: __dirname,
Expand Down

0 comments on commit 2bcaadd

Please sign in to comment.