1 | "use strict";
|
---|
2 | /**
|
---|
3 | * @license
|
---|
4 | * Copyright Google LLC All Rights Reserved.
|
---|
5 | *
|
---|
6 | * Use of this source code is governed by an MIT-style license that can be
|
---|
7 | * found in the LICENSE file at https://angular.io/license
|
---|
8 | */
|
---|
9 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
10 | exports.mergeSchemas = exports.isJsonSchema = void 0;
|
---|
11 | const interface_1 = require("../interface");
|
---|
12 | function isJsonSchema(value) {
|
---|
13 | return interface_1.isJsonObject(value) || value === false || value === true;
|
---|
14 | }
|
---|
15 | exports.isJsonSchema = isJsonSchema;
|
---|
16 | /**
|
---|
17 | * Return a schema that is the merge of all subschemas, ie. it should validate all the schemas
|
---|
18 | * that were passed in. It is possible to make an invalid schema this way, e.g. by using
|
---|
19 | * `mergeSchemas({ type: 'number' }, { type: 'string' })`, which will never validate.
|
---|
20 | * @param schemas All schemas to be merged.
|
---|
21 | */
|
---|
22 | function mergeSchemas(...schemas) {
|
---|
23 | return schemas.reduce((prev, curr) => {
|
---|
24 | if (curr === undefined) {
|
---|
25 | return prev;
|
---|
26 | }
|
---|
27 | if (prev === false || curr === false) {
|
---|
28 | return false;
|
---|
29 | }
|
---|
30 | else if (prev === true) {
|
---|
31 | return curr;
|
---|
32 | }
|
---|
33 | else if (curr === true) {
|
---|
34 | return prev;
|
---|
35 | }
|
---|
36 | else if (Array.isArray(prev.allOf)) {
|
---|
37 | if (Array.isArray(curr.allOf)) {
|
---|
38 | return { ...prev, allOf: [...prev.allOf, ...curr.allOf] };
|
---|
39 | }
|
---|
40 | else {
|
---|
41 | return { ...prev, allOf: [...prev.allOf, curr] };
|
---|
42 | }
|
---|
43 | }
|
---|
44 | else if (Array.isArray(curr.allOf)) {
|
---|
45 | return { ...prev, allOf: [prev, ...curr.allOf] };
|
---|
46 | }
|
---|
47 | else {
|
---|
48 | return { ...prev, allOf: [prev, curr] };
|
---|
49 | }
|
---|
50 | }, true);
|
---|
51 | }
|
---|
52 | exports.mergeSchemas = mergeSchemas;
|
---|