UNPKG

1.66 kBJavaScriptView Raw
1"use strict";
2// Copyright IBM Corp. and LoopBack contributors 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.ArrayType = 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 * Array type, such as string[]
13 */
14class ArrayType {
15 constructor(itemType) {
16 this.itemType = itemType;
17 this.name = 'array';
18 }
19 isInstance(value) {
20 if (value == null)
21 return true;
22 if (!Array.isArray(value)) {
23 return false;
24 }
25 const list = value;
26 return list.every(i => this.itemType.isInstance(i));
27 }
28 isCoercible(value) {
29 if (value == null)
30 return true;
31 if (!Array.isArray(value)) {
32 return false;
33 }
34 return value.every(i => this.itemType.isCoercible(i));
35 }
36 defaultValue() {
37 return [];
38 }
39 coerce(value) {
40 if (value == null)
41 return value;
42 if (!Array.isArray(value)) {
43 const msg = util_1.default.format('Invalid %s: %j', this.name, value);
44 throw new TypeError(msg);
45 }
46 return value.map(i => this.itemType.coerce(i));
47 }
48 serialize(value) {
49 if (value == null)
50 return value;
51 return value.map(i => this.itemType.serialize(i));
52 }
53}
54exports.ArrayType = ArrayType;
55//# sourceMappingURL=array.js.map
\No newline at end of file