Skip to content

Commit

Permalink
feat: remove support for callback parameter from compileAsync
Browse files Browse the repository at this point in the history
  • Loading branch information
epoberezkin committed Sep 13, 2020
1 parent a4d115d commit 945190d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 93 deletions.
13 changes: 2 additions & 11 deletions lib/ajv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ const EXT_SCOPE_NAMES = new Set([
"Error",
])

type CompileAsyncCallback = (err: Error | null, validate?: ValidateFunction) => void

const optsDefaults = {
strict: true,
code: {},
Expand Down Expand Up @@ -135,20 +133,13 @@ export default class Ajv {
// TODO allow passing schema URI
compileAsync(
schema: SchemaObject,
metaOrCallback?: boolean | CompileAsyncCallback, // optional true to compile meta-schema; this parameter can be skipped
cb?: CompileAsyncCallback // deprecated
meta?: boolean // optional true to compile meta-schema
): Promise<ValidateFunction> {
if (typeof this._opts.loadSchema != "function") {
throw new Error("options.loadSchema should be a function")
}
const {loadSchema} = this._opts
let meta: boolean | undefined
if (typeof metaOrCallback == "function") cb = metaOrCallback
else meta = metaOrCallback

const vp = runCompileAsync.call(this, schema, meta)
if (cb) vp.then((v) => cb?.(null, v), cb)
return vp
return runCompileAsync.call(this, schema, meta)

async function runCompileAsync(
this: Ajv,
Expand Down
99 changes: 17 additions & 82 deletions spec/async.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import _Ajv from "./ajv"
import type {SchemaObject} from "../dist/types"
import type {SchemaObject, ValidateFunction} from "../dist/types"

const should = require("./chai").should()

Expand Down Expand Up @@ -99,20 +99,18 @@ describe("compileAsync method", () => {
})
})

it("should compile schemas loading missing schemas and return function via callback", (done) => {
it("should compile schemas loading missing schemas and return promise with function", () => {
const schema = {
$id: "http://example.com/parent.json",
properties: {
a: {$ref: "object.json"},
},
}
ajv.compileAsync(schema, (err, validate) => {
return ajv.compileAsync(schema).then((validate) => {
should.equal(loadCallCount, 2)
should.not.exist(err)
validate.should.be.a("function")
validate({a: {b: 2}}).should.equal(true)
validate({a: {b: 1}}).should.equal(false)
done()
})
})

Expand Down Expand Up @@ -263,85 +261,19 @@ describe("compileAsync method", () => {
}
})

it("should throw exception if loadSchema is not passed", (done) => {
it("should throw exception if loadSchema is not passed", () => {
const schema = {
$id: "http://example.com/int2plus.json",
type: "integer",
minimum: 2,
}
ajv = new _Ajv()
should.throw(() => {
ajv.compileAsync(schema, () => {
done(new Error("it should have thrown exception"))
})
ajv.compileAsync(schema).then(expextedSyncError, expextedSyncError)
})
setTimeout(() => {
// function is needed for the test to pass in Firefox 4
done()
})
})

describe("should return error via callback", () => {
it("if passed schema is invalid", (done) => {
const invalidSchema = {
$id: "http://example.com/int2plus.json",
type: "integer",
minimum: "invalid",
}
ajv.compileAsync(invalidSchema, shouldFail(done))
})

it("if loaded schema is invalid", (done) => {
const schema = {
$id: "http://example.com/parent.json",
properties: {
a: {$ref: "invalid.json"},
},
}
ajv.compileAsync(schema, shouldFail(done))
})

it("if required schema is loaded but the reference cannot be resolved", (done) => {
const schema = {
$id: "http://example.com/parent.json",
properties: {
a: {$ref: "object.json#/definitions/not_found"},
},
}
ajv.compileAsync(schema, shouldFail(done))
})

it("if loadSchema returned error", (done) => {
const schema = {
$id: "http://example.com/parent.json",
properties: {
a: {$ref: "object.json"},
},
}
ajv = new _Ajv({loadSchema: badLoadSchema})
ajv.compileAsync(schema, shouldFail(done))

function badLoadSchema() {
return Promise.reject(new Error("cant load"))
}
})

it("if schema compilation throws some other exception", (done) => {
ajv.addKeyword({keyword: "badkeyword", compile: badCompile})
const schema = {badkeyword: true}
ajv.compileAsync(schema, shouldFail(done))

function badCompile(/* schema */) {
throw new Error("cant compile keyword schema")
}
})

function shouldFail(done) {
return (err, validate) => {
should.exist(err)
should.not.exist(validate)
done()
}
function expextedSyncError() {
throw new Error("it should have thrown exception")
}
})

Expand All @@ -352,7 +284,7 @@ describe("compileAsync method", () => {
type: "integer",
minimum: "invalid",
}
return shouldReject(ajv.compileAsync(invalidSchema))
return shouldReject(ajv.compileAsync(invalidSchema), /schema is invalid/)
})

it("if loaded schema is invalid", () => {
Expand All @@ -362,7 +294,7 @@ describe("compileAsync method", () => {
a: {$ref: "invalid.json"},
},
}
return shouldReject(ajv.compileAsync(schema))
return shouldReject(ajv.compileAsync(schema), /schema is invalid/)
})

it("if required schema is loaded but the reference cannot be resolved", () => {
Expand All @@ -372,7 +304,7 @@ describe("compileAsync method", () => {
a: {$ref: "object.json#/definitions/not_found"},
},
}
return shouldReject(ajv.compileAsync(schema))
return shouldReject(ajv.compileAsync(schema), /is loaded but/)
})

it("if loadSchema returned error", () => {
Expand All @@ -383,7 +315,7 @@ describe("compileAsync method", () => {
},
}
ajv = new _Ajv({loadSchema: badLoadSchema})
return shouldReject(ajv.compileAsync(schema))
return shouldReject(ajv.compileAsync(schema), /cant load/)

function badLoadSchema() {
return Promise.reject(new Error("cant load"))
Expand All @@ -393,20 +325,23 @@ describe("compileAsync method", () => {
it("if schema compilation throws some other exception", () => {
ajv.addKeyword({keyword: "badkeyword", compile: badCompile})
const schema = {badkeyword: true}
return shouldReject(ajv.compileAsync(schema))
return shouldReject(ajv.compileAsync(schema), /cant compile keyword schema/)

function badCompile(/* schema */) {
throw new Error("cant compile keyword schema")
}
})

function shouldReject(p) {
function shouldReject(p: Promise<ValidateFunction>, rx: RegExp) {
return p.then(
(validate) => {
should.not.exist(validate)
throw new Error("Promise has resolved; it should have rejected")
},
(err) => should.exist(err)
(err) => {
should.exist(err)
err.message.should.match(rx)
}
)
}
})
Expand Down

0 comments on commit 945190d

Please sign in to comment.