Skip to content

Commit 76b3199

Browse files
committed
🦺 use errorLogger instead of console log
1 parent cdd7a9f commit 76b3199

File tree

2 files changed

+54
-11
lines changed

2 files changed

+54
-11
lines changed

‎src/models/controller.model.ts‎

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,9 @@ export class Controller {
187187
providerConfig,
188188
);
189189

190-
if (!validate(this.ajv, contactsSchema, fetchedContacts)) {
190+
if (
191+
!validate(this.ajv, contactsSchema, fetchedContacts, providerConfig)
192+
) {
191193
throw new ServerError(500, 'Invalid contacts received');
192194
}
193195

@@ -297,7 +299,7 @@ export class Controller {
297299

298300
const publishContacts = async (contacts: Contact[]) => {
299301
try {
300-
if (!validate(this.ajv, contactsSchema, contacts)) {
302+
if (!validate(this.ajv, contactsSchema, contacts, providerConfig)) {
301303
throw new Error('Invalid contacts received');
302304
}
303305

@@ -453,7 +455,14 @@ export class Controller {
453455
parseInt(req.params.timestamp),
454456
);
455457

456-
if (!validate(this.ajv, contactsSchema, fetchedDelta.contacts)) {
458+
if (
459+
!validate(
460+
this.ajv,
461+
contactsSchema,
462+
fetchedDelta.contacts,
463+
providerConfig,
464+
)
465+
) {
457466
throw new ServerError(500, 'Invalid contacts received');
458467
}
459468

@@ -577,7 +586,12 @@ export class Controller {
577586
req.body,
578587
);
579588

580-
const valid = validate(this.ajv, contactsSchema, [contact]);
589+
const valid = validate(
590+
this.ajv,
591+
contactsSchema,
592+
[contact],
593+
req.providerConfig,
594+
);
581595

582596
if (!valid) {
583597
errorLogger(
@@ -655,7 +669,12 @@ export class Controller {
655669
req.body,
656670
);
657671

658-
const valid = validate(this.ajv, contactsSchema, [contact]);
672+
const valid = validate(
673+
this.ajv,
674+
contactsSchema,
675+
[contact],
676+
req.providerConfig,
677+
);
659678
if (!valid) {
660679
errorLogger(
661680
'updateContact',
@@ -814,7 +833,12 @@ export class Controller {
814833
const calendarEvents: CalendarEvent[] =
815834
await this.adapter.getCalendarEvents(req.providerConfig, filter);
816835

817-
const valid = validate(this.ajv, calendarEventsSchema, calendarEvents);
836+
const valid = validate(
837+
this.ajv,
838+
calendarEventsSchema,
839+
calendarEvents,
840+
req.providerConfig,
841+
);
818842
if (!valid) {
819843
errorLogger(
820844
'getCalendarEvents',
@@ -871,7 +895,12 @@ export class Controller {
871895
const calendarEvent: CalendarEvent =
872896
await this.adapter.createCalendarEvent(req.providerConfig, req.body);
873897

874-
const valid = validate(this.ajv, calendarEventsSchema, [calendarEvent]);
898+
const valid = validate(
899+
this.ajv,
900+
calendarEventsSchema,
901+
[calendarEvent],
902+
req.providerConfig,
903+
);
875904
if (!valid) {
876905
errorLogger(
877906
'createCalendarEvent',
@@ -925,7 +954,12 @@ export class Controller {
925954
req.body,
926955
);
927956

928-
const valid = validate(this.ajv, calendarEventsSchema, [calendarEvent]);
957+
const valid = validate(
958+
this.ajv,
959+
calendarEventsSchema,
960+
[calendarEvent],
961+
req.providerConfig,
962+
);
929963
if (!valid) {
930964
errorLogger(
931965
'updateCalendarEvent',

‎src/util/validate.ts‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,29 @@
11
import Ajv from 'ajv';
2+
import { Config } from '../models';
3+
import { errorLogger } from './logger.util';
24

35
export function validate(
46
ajv: Ajv,
57
schemaKeyRef: object | string | boolean,
68
data: object,
9+
config: Config,
710
) {
811
try {
912
const valid: boolean | PromiseLike<boolean> = ajv.validate(
1013
schemaKeyRef,
1114
data,
1215
);
1316
if (!valid) {
14-
console.error('Invalid data provided by adapter', ajv.errorsText());
17+
errorLogger(
18+
'validate',
19+
'Validation failed',
20+
config?.apiKey,
21+
ajv.errorsText(),
22+
);
23+
return false;
1524
}
16-
// console.log("Adapter data is", {valid});
17-
return valid;
25+
26+
return true;
1827
} catch (e) {
1928
console.error('Error validating data', e, ajv.errorsText());
2029
// Ignore validation if validation is broken

0 commit comments

Comments
 (0)