1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | import debugModule from 'debug';
|
7 | const debug = debugModule('loopback:rest:coercion');
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | export type DateCoercionOptions = {
|
13 | dateOnly?: boolean;
|
14 | };
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | export type IntegerCoercionOptions = {
|
20 | isLong?: boolean;
|
21 | };
|
22 |
|
23 | export function isEmpty(data: string) {
|
24 | const result = data === '';
|
25 | debug('isEmpty(%j) -> %s', data, result);
|
26 | return result;
|
27 | }
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 | export function isTrue(data: string): boolean {
|
35 | return ['TRUE', '1'].includes(data.toUpperCase());
|
36 | }
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 | export function isFalse(data: string): boolean {
|
44 | return ['FALSE', '0'].includes(data.toUpperCase());
|
45 | }
|
46 |
|
47 |
|
48 |
|
49 |
|
50 | export function isValidDateTime(data: Date) {
|
51 | return isNaN(data.getTime()) ? false : true;
|
52 | }
|
53 |
|
54 | const REGEX_RFC3339_DATE =
|
55 | /^(-?(?:[1-9][0-9]*)?[0-9]{4})-(1[0-2]|0[1-9])-(3[01]|0[1-9]|[12][0-9])$/;
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 | export function matchDateFormat(date: string) {
|
63 | const pattern = new RegExp(REGEX_RFC3339_DATE);
|
64 | const result = pattern.test(date);
|
65 | debug('matchDateFormat(%j) -> %s', date, result);
|
66 | return result;
|
67 | }
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 | export function getOAIPrimitiveType(type?: string, format?: string) {
|
76 | if (type === 'object' || type === 'array') return type;
|
77 | if (type === 'string') {
|
78 | switch (format) {
|
79 | case 'byte':
|
80 | return 'byte';
|
81 | case 'binary':
|
82 | return 'binary';
|
83 | case 'date':
|
84 | return 'date';
|
85 | case 'date-time':
|
86 | return 'date-time';
|
87 | case 'password':
|
88 | return 'password';
|
89 | default:
|
90 | return 'string';
|
91 | }
|
92 | }
|
93 | if (type === 'boolean') return 'boolean';
|
94 | if (type === 'number')
|
95 | switch (format) {
|
96 | case 'float':
|
97 | return 'float';
|
98 | case 'double':
|
99 | return 'double';
|
100 | default:
|
101 | return 'number';
|
102 | }
|
103 | if (type === 'integer') return format === 'int64' ? 'long' : 'integer';
|
104 | }
|