UNPKG

1.51 kBJavaScriptView Raw
1"use strict";
2// Copyright IBM Corp. 2017,2019. All Rights Reserved.
3// Node module: @loopback/repository
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.ObjectType = void 0;
8const tslib_1 = require("tslib");
9const util_1 = tslib_1.__importDefault(require("util"));
10/* eslint-disable @typescript-eslint/no-explicit-any */
11/**
12 * Object type
13 */
14class ObjectType {
15 constructor(type) {
16 this.type = type;
17 this.name = 'object';
18 }
19 isInstance(value) {
20 return value == null || value instanceof this.type;
21 }
22 isCoercible(value) {
23 return (value == null || (typeof value === 'object' && !Array.isArray(value)));
24 }
25 defaultValue() {
26 return new this.type();
27 }
28 coerce(value) {
29 if (value == null)
30 return value;
31 if (value instanceof this.type) {
32 return value;
33 }
34 if (typeof value !== 'object' || Array.isArray(value)) {
35 const msg = util_1.default.format('Invalid %s: %j', this.name, value);
36 throw new TypeError(msg);
37 }
38 return new this.type(value);
39 }
40 serialize(value) {
41 if (value == null)
42 return value;
43 if (typeof value.toJSON === 'function') {
44 return value.toJSON();
45 }
46 return value;
47 }
48}
49exports.ObjectType = ObjectType;
50//# sourceMappingURL=object.js.map
\No newline at end of file