Skip to content

Commit

Permalink
Add overloaded signatures for sync/async compile
Browse files Browse the repository at this point in the history
  • Loading branch information
villasv committed Sep 10, 2020
1 parent eb38f57 commit 63e5ee6
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 0 deletions.
28 changes: 28 additions & 0 deletions lib/ajv.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import {
Schema,
SchemaObject,
SyncSchemaObject,
AsyncSchemaObject,
Vocabulary,
KeywordDefinition,
Options,
ValidateFunction,
SyncValidateFunction,
AsyncValidateFunction,
CacheInterface,
Logger,
ErrorObject,
Expand Down Expand Up @@ -107,6 +111,10 @@ export default class Ajv {
}

// Create validation function for passed schema
compile(s: {$async?: never}, _?: boolean): ValidateFunction
compile(s: SyncSchemaObject, _?: boolean): SyncValidateFunction
compile(s: AsyncSchemaObject, _?: boolean): AsyncValidateFunction
compile(s: Schema, _?: boolean): ValidateFunction
compile(
schema: Schema,
_meta?: boolean // true if schema is a meta-schema. Used internally to compile meta schemas of custom keywords.
Expand All @@ -117,6 +125,26 @@ export default class Ajv {

// Creates validating function for passed schema with asynchronous loading of missing schemas.
// `loadSchema` option should be a function that accepts schema uri and returns promise that resolves with the schema.
compileAsync(
s: {$async?: never},
m?: boolean | CompileAsyncCallback,
c?: CompileAsyncCallback
): Promise<ValidateFunction>
compileAsync(
s: SyncSchemaObject,
m?: boolean | CompileAsyncCallback,
c?: CompileAsyncCallback
): Promise<SyncValidateFunction>
compileAsync(
s: AsyncSchemaObject,
m?: boolean | CompileAsyncCallback,
c?: CompileAsyncCallback
): Promise<AsyncValidateFunction>
compileAsync(
s: SchemaObject,
m?: boolean | CompileAsyncCallback,
c?: CompileAsyncCallback
): Promise<ValidateFunction>
compileAsync(
schema: SchemaObject,
metaOrCallback?: boolean | CompileAsyncCallback, // optional true to compile meta-schema; this parameter can be skipped
Expand Down
8 changes: 8 additions & 0 deletions lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ export interface ValidateFunction {
source?: SourceCode
}

export interface SyncSchemaObject extends SchemaObject {
$async: false
}

export interface SyncValidateFunction extends ValidateFunction {
(
this: Ajv | any,
Expand All @@ -118,6 +122,10 @@ export interface SyncValidateFunction extends ValidateFunction {
$async: undefined
}

export interface AsyncSchemaObject extends SchemaObject {
$async: true
}

export interface AsyncValidateFunction extends ValidateFunction {
(
this: Ajv | any,
Expand Down
73 changes: 73 additions & 0 deletions spec/options/async.spec.ts
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)
}
})
})
})

0 comments on commit 63e5ee6

Please sign in to comment.