UNPKG

2.28 kBJavaScriptView Raw
1"use strict";
2// Copyright IBM Corp. and LoopBack contributors 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.openapiFormats = exports.binaryFormat = exports.byteFormat = exports.doubleFormat = exports.floatFormat = exports.int64Format = exports.int32Format = void 0;
8/**
9 * int32: [-2147483648, 21474836 47]
10 */
11exports.int32Format = {
12 name: 'int32',
13 type: 'number',
14 validate: (value) => {
15 return (Number.isInteger(value) && value >= -2147483648 && value <= 2147483647);
16 },
17 async: false,
18};
19/**
20 * int64: [-9223372036854775808, 9223372036854775807]
21 */
22exports.int64Format = {
23 name: 'int64',
24 type: 'number',
25 validate: (value) => {
26 const max = Number.MAX_SAFE_INTEGER; // 9007199254740991
27 const min = Number.MIN_SAFE_INTEGER; // -9007199254740991
28 return Number.isInteger(value) && value >= min && value <= max;
29 },
30 async: false,
31};
32/**
33 * float: [-2^128, 2^128]
34 */
35exports.floatFormat = {
36 name: 'float',
37 type: 'number',
38 validate: (value) => {
39 return value >= -Math.pow(2, 128) && value <= Math.pow(2, 128);
40 },
41 async: false,
42};
43/**
44 * double: [-2^1024, 2^1024]
45 */
46exports.doubleFormat = {
47 name: 'double',
48 type: 'number',
49 validate: (value) => {
50 const max = Number.MAX_VALUE; // 1.7976931348623157e+308
51 const min = -Number.MAX_VALUE; // -1.7976931348623157e+308
52 return value >= min && value <= max;
53 },
54 async: false,
55};
56/**
57 * Base64 encoded string
58 */
59exports.byteFormat = {
60 name: 'byte',
61 type: 'string',
62 validate: (value) => {
63 const base64 = Buffer.from(value, 'base64').toString('base64');
64 return value === base64;
65 },
66 async: false,
67};
68/**
69 * Binary string
70 */
71exports.binaryFormat = {
72 name: 'binary',
73 type: 'string',
74 validate: (value) => true,
75 async: false,
76};
77exports.openapiFormats = [
78 exports.int32Format,
79 exports.int64Format,
80 exports.floatFormat,
81 exports.doubleFormat,
82 exports.byteFormat,
83 exports.binaryFormat,
84];
85//# sourceMappingURL=openapi-formats.js.map
\No newline at end of file