Commit | Line | Data |
---|---|---|
6c1761d4 | 1 | import type { DefinedError, ErrorObject } from 'ajv'; |
06ad945f JB |
2 | |
3 | import { ErrorType } from '../../types/ocpp/ErrorType'; | |
4 | ||
90befdb8 | 5 | export class OCPPServiceUtils { |
d5bd1c00 JB |
6 | protected constructor() { |
7 | // This is intentional | |
8 | } | |
9 | ||
01a4dcbb | 10 | public static ajvErrorsToErrorType(errors: ErrorObject[]): ErrorType { |
06ad945f JB |
11 | for (const error of errors as DefinedError[]) { |
12 | switch (error.keyword) { | |
13 | case 'type': | |
14 | return ErrorType.TYPE_CONSTRAINT_VIOLATION; | |
15 | case 'dependencies': | |
16 | case 'required': | |
17 | return ErrorType.OCCURRENCE_CONSTRAINT_VIOLATION; | |
18 | case 'pattern': | |
19 | case 'format': | |
20 | return ErrorType.PROPERTY_CONSTRAINT_VIOLATION; | |
21 | } | |
22 | } | |
23 | return ErrorType.FORMAT_VIOLATION; | |
24 | } | |
25 | ||
90befdb8 JB |
26 | protected static getLimitFromSampledValueTemplateCustomValue( |
27 | value: string, | |
28 | limit: number, | |
29 | options: { limitationEnabled?: boolean; unitMultiplier?: number } = { | |
30 | limitationEnabled: true, | |
31 | unitMultiplier: 1, | |
32 | } | |
33 | ): number { | |
34 | options.limitationEnabled = options?.limitationEnabled ?? true; | |
35 | options.unitMultiplier = options?.unitMultiplier ?? 1; | |
f126aa15 | 36 | const numberValue = isNaN(parseInt(value)) ? Infinity : parseInt(value); |
90befdb8 | 37 | return options?.limitationEnabled |
f126aa15 JB |
38 | ? Math.min(numberValue * options.unitMultiplier, limit) |
39 | : numberValue * options.unitMultiplier; | |
90befdb8 JB |
40 | } |
41 | } |