-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
43 lines (36 loc) · 1.11 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import type { Ajv, Options as AjvOptions } from "ajv";
import {
int32,
int64,
float,
double,
byte
} from "./lib/validators";
export = ajvOpenApi;
function ajvOpenApi(ajv: Ajv, options?: ajvOpenApi.AjvOpenApiOptions): Ajv {
if (options?.useDraft04 !== false) {
ajv.addMetaSchema(require("ajv/lib/refs/json-schema-draft-04.json"));
}
ajv.addFormat("int32", { type: "number", validate: int32 });
ajv.addFormat("int64", { type: "number", validate: int64 });
ajv.addFormat("float", { type: "number", validate: float });
ajv.addFormat("double", { type: "number", validate: double });
ajv.addFormat("byte", { type: "string", validate: byte });
return ajv;
}
namespace ajvOpenApi {
export interface AjvOpenApiOptions {
useDraft04?: boolean;
}
export function createOptions(options?: AjvOptions): AjvOptions {
return {
schemaId: "auto",
format: "full",
coerceTypes: true,
unknownFormats: "ignore",
useDefaults: true,
nullable: true,
...options
};
}
}