-
Notifications
You must be signed in to change notification settings - Fork 2
/
utilities.ts
25 lines (22 loc) · 922 Bytes
/
utilities.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
import Ajv, { Schema } from 'ajv/dist/2020';
export const isValidSchema = (schema: Schema, example: unknown): boolean => {
const ajv = new Ajv({
strict: false,
verbose: true,
strictSchema: 'log',
allowUnionTypes: true,
});
const validate = ajv.compile(schema);
const result = validate(example);
if (!result) {
console.error('Validation Errors: ', validate.errors);
}
return result;
};
export const getValidationWithDependencies = (schema: Schema, dependencies: Schema[], example: unknown): boolean => {
// When dealing with multiple schemas, you need to make sure they are all loaded before trying to validate
// Currently, DeviceShadow depends on Config being loaded, so you would need to call addSchema with Config
const ajv = new Ajv();
const validate = ajv.addSchema(dependencies).compile(schema);
return validate(example);
};