-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.ts
48 lines (41 loc) · 1.34 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
44
45
46
47
48
import Ajv, { _ } from "ajv"
import addFormats from "ajv-formats"
import { Validators } from "../.."
// personBasic + dob type coercion
import personSchema from "./schemas/person.json"
// defines person schema, but without type coercion, as the custom keyword is not written with code generation
import personBasicSchema from "./schemas/person-basic.json"
import personFormSchema from "./schemas/personForm.json"
import driverSchema from "./schemas/driver.json"
import vehicleSchema from "./schemas/vehicle.json"
import fleetSchema from "./schemas/fleet.json"
import addKeyword from "./customer-keywords"
const ajv = new Ajv({
allErrors: true,
$data: true,
schemas: [
personSchema, personBasicSchema, personFormSchema, driverSchema, vehicleSchema, fleetSchema
],
coerceTypes:true,
useDefaults: true
});
addFormats(ajv)
addKeyword(ajv)
const validator = (schema:any):(data:unknown)=>any=>(
(data)=>{
const validator = ajv.getSchema(schema.$id)!;
if (validator(data)) {
return data
} else {
throw validator.errors
}
}
)
const validators: Validators = {
person: validator(personSchema),
driver: validator(driverSchema),
fleet: validator(fleetSchema),
vehicle: validator(vehicleSchema),
personForm: validator(personFormSchema)
}
export default validators