-
-
Notifications
You must be signed in to change notification settings - Fork 872
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add overloaded signatures for sync/async compile
- Loading branch information
Showing
3 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import _Ajv from "../ajv" | ||
require("../chai").should() | ||
|
||
describe("$async option", () => { | ||
const ajv = new _Ajv() | ||
|
||
describe("= undefined", () => { | ||
const validate = ajv.compile({}) | ||
it("should return a boolean or promise", async () => { | ||
const result = validate({}) | ||
if (typeof result === "boolean") { | ||
result.should.exist | ||
} else { | ||
await result.then((data) => data.should.exist) | ||
} | ||
}) | ||
}) | ||
|
||
describe("= false", () => { | ||
const validate = ajv.compile({$async: false}) | ||
it("should return a boolean", () => { | ||
const result: boolean = validate({}) | ||
result.should.exist | ||
}) | ||
}) | ||
|
||
describe("= true", () => { | ||
const validate = ajv.compile({$async: true}) | ||
it("should return a promise", async () => { | ||
const result: Promise<any> = validate({}) | ||
await result.then((data) => data.should.exist) | ||
}) | ||
}) | ||
|
||
describe("= boolean", () => { | ||
const schema = {$async: true} | ||
const validate = ajv.compile(schema) | ||
it("should return boolean or promise", async () => { | ||
const result = validate({}) | ||
if (typeof result === "boolean") { | ||
result.should.exist | ||
} else { | ||
await result.then((data) => data.should.exist) | ||
} | ||
}) | ||
}) | ||
|
||
describe("of type unknown", () => { | ||
const schema: Record<string, unknown> = {} | ||
const validate = ajv.compile(schema) | ||
it("should return boolean or promise", async () => { | ||
const result = validate({}) | ||
if (typeof result === "boolean") { | ||
result.should.exist | ||
} else { | ||
await result.then((data) => data.should.exist) | ||
} | ||
}) | ||
}) | ||
|
||
describe("of type any", () => { | ||
const schema: any = {} | ||
const validate = ajv.compile(schema) | ||
it("should return boolean or promise", async () => { | ||
const result = validate({}) | ||
if (typeof result === "boolean") { | ||
result.should.exist | ||
} else { | ||
await result.then((data) => data.should.exist) | ||
} | ||
}) | ||
}) | ||
}) |