UNPKG

3.16 kBJavaScriptView Raw
1"use strict";
2// Copyright IBM Corp. and LoopBack contributors 2018,2020. All Rights Reserved.
3// Node module: @loopback/rest
4// This file is licensed under the MIT License.
5// License text available at https://opensource.org/licenses/MIT
6Object.defineProperty(exports, "__esModule", { value: true });
7exports.getOAIPrimitiveType = exports.matchDateFormat = exports.isValidDateTime = exports.isFalse = exports.isTrue = exports.isEmpty = void 0;
8const tslib_1 = require("tslib");
9const debug_1 = tslib_1.__importDefault(require("debug"));
10const debug = (0, debug_1.default)('loopback:rest:coercion');
11function isEmpty(data) {
12 const result = data === '';
13 debug('isEmpty(%j) -> %s', data, result);
14 return result;
15}
16exports.isEmpty = isEmpty;
17/**
18 * A set of truthy values. A data in this set will be coerced to `true`.
19 *
20 * @param data - The raw data get from http request
21 * @returns The corresponding coerced boolean type
22 */
23function isTrue(data) {
24 return ['TRUE', '1'].includes(data.toUpperCase());
25}
26exports.isTrue = isTrue;
27/**
28 * A set of falsy values. A data in this set will be coerced to `false`.
29 * @param data - The raw data get from http request
30 * @returns The corresponding coerced boolean type
31 */
32function isFalse(data) {
33 return ['FALSE', '0'].includes(data.toUpperCase());
34}
35exports.isFalse = isFalse;
36/**
37 * Return false for invalid date
38 */
39function isValidDateTime(data) {
40 return isNaN(data.getTime()) ? false : true;
41}
42exports.isValidDateTime = isValidDateTime;
43const REGEX_RFC3339_DATE = /^(-?(?:[1-9][0-9]*)?[0-9]{4})-(1[0-2]|0[1-9])-(3[01]|0[1-9]|[12][0-9])$/;
44/**
45 * Return true when a date follows the RFC3339 standard
46 *
47 * @param date - The date to verify
48 */
49function matchDateFormat(date) {
50 const pattern = new RegExp(REGEX_RFC3339_DATE);
51 const result = pattern.test(date);
52 debug('matchDateFormat(%j) -> %s', date, result);
53 return result;
54}
55exports.matchDateFormat = matchDateFormat;
56/**
57 * Return the corresponding OpenAPI data type given an OpenAPI schema
58 *
59 * @param type - The type in an OpenAPI schema specification
60 * @param format - The format in an OpenAPI schema specification
61 */
62function getOAIPrimitiveType(type, format) {
63 if (type === 'object' || type === 'array')
64 return type;
65 if (type === 'string') {
66 switch (format) {
67 case 'byte':
68 return 'byte';
69 case 'binary':
70 return 'binary';
71 case 'date':
72 return 'date';
73 case 'date-time':
74 return 'date-time';
75 case 'password':
76 return 'password';
77 default:
78 return 'string';
79 }
80 }
81 if (type === 'boolean')
82 return 'boolean';
83 if (type === 'number')
84 switch (format) {
85 case 'float':
86 return 'float';
87 case 'double':
88 return 'double';
89 default:
90 return 'number';
91 }
92 if (type === 'integer')
93 return format === 'int64' ? 'long' : 'integer';
94}
95exports.getOAIPrimitiveType = getOAIPrimitiveType;
96//# sourceMappingURL=utils.js.map
\No newline at end of file