Skip to content

Commit f2d9800

Browse files
committed
🏷️ add Type for schema validation
1 parent a475e6f commit f2d9800

File tree

4 files changed

+26
-6
lines changed

4 files changed

+26
-6
lines changed

src/schemas/calendar-events.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
export const calendarEventsSchema = {
1+
import { ValidationSchema } from './schema.model';
2+
3+
export const calendarEventsSchema: ValidationSchema = {
24
title: 'CalendarEvents',
35
type: 'array',
46
items: {

src/schemas/contacts.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
export const contactsSchema = {
1+
import { ValidationSchema } from './schema.model';
2+
3+
export const contactsSchema: ValidationSchema = {
24
title: 'Contacts',
35
type: 'array',
46
items: {

src/schemas/schema.model.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export type ValidationSchema = {
2+
title: string;
3+
type: string;
4+
items: {
5+
type: string;
6+
properties: unknown;
7+
required: string[];
8+
};
9+
};

src/util/validate.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import Ajv from 'ajv';
22
import { Config } from '../models';
33
import { errorLogger } from './logger.util';
4+
import { ValidationSchema } from '../schemas/schema.model';
45

56
export function validate(
67
ajv: Ajv,
7-
schemaKeyRef: object | string | boolean,
8+
schemaKeyRef: ValidationSchema,
89
data: object,
910
config: Config,
1011
) {
@@ -16,16 +17,22 @@ export function validate(
1617
if (!valid) {
1718
errorLogger(
1819
'validate',
19-
'Validation failed',
20-
config?.apiKey,
20+
`${schemaKeyRef.type}: validation failed`,
21+
config.apiKey,
2122
ajv.errorsText(),
2223
);
2324
return false;
2425
}
2526

2627
return true;
2728
} catch (e) {
28-
console.error('Error validating data', e, ajv.errorsText());
29+
errorLogger(
30+
'validate',
31+
'Error validating data',
32+
config.apiKey,
33+
e,
34+
ajv.errorsText(),
35+
);
2936
// Ignore validation if validation is broken
3037
return true;
3138
}

0 commit comments

Comments
 (0)