1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | Object.defineProperty(exports, "__esModule", { value: true });
|
7 | exports.ObjectType = void 0;
|
8 | const tslib_1 = require("tslib");
|
9 | const util_1 = tslib_1.__importDefault(require("util"));
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | class 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 | }
|
49 | exports.ObjectType = ObjectType;
|
50 |
|
\ | No newline at end of file |