|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch B.V. under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch B.V. licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +import assert from 'assert' |
| 21 | +import * as model from '../model/metamodel' |
| 22 | +import { JsonSpec } from '../model/json-spec' |
| 23 | +import chalk from 'chalk' |
| 24 | + |
| 25 | +/** |
| 26 | + * Verifies if "read" version of interface definitions |
| 27 | + * contains the same properties of their "write" version. |
| 28 | + * Then, it copies every model.Property from write to read but 'required'. |
| 29 | + */ |
| 30 | +export default async function readDefinitionValidation (model: model.Model, jsonSpec: Map<string, JsonSpec>): Promise<model.Model> { |
| 31 | + for (const type of model.types) { |
| 32 | + if (type.kind !== 'interface') continue |
| 33 | + const readBehavior = type.behaviors?.find(behavior => behavior.type.name === 'OverloadOf') |
| 34 | + if (readBehavior == null) continue |
| 35 | + assert(Array.isArray(readBehavior.generics)) |
| 36 | + assert(readBehavior.generics[0].kind === 'instance_of') |
| 37 | + |
| 38 | + for (const parent of model.types) { |
| 39 | + if (parent.name.name === readBehavior.generics[0].type.name && parent.name.namespace === readBehavior.generics[0].type.namespace) { |
| 40 | + assert(parent.kind === 'interface') |
| 41 | + const readProperties = type.properties.map(p => p.name) |
| 42 | + for (const property of parent.properties) { |
| 43 | + // have we defined the same properties? |
| 44 | + if (!readProperties.includes(property.name)) { |
| 45 | + console.log(chalk.red`The property '${property.name}' is present in ${parent.name.namespace}.${parent.name.name} but not in ${type.name.namespace}.${type.name.name}`) |
| 46 | + process.exit(1) |
| 47 | + } |
| 48 | + |
| 49 | + const readProperty = type.properties.find(p => p.name === property.name) as model.Property |
| 50 | + // in the future we might want to have a different type between read and write defintions |
| 51 | + // but let's address it when it will happen |
| 52 | + if (!deepEqual(readProperty.type, property.type)) { |
| 53 | + console.log(chalk.red`The property '${property.name}' present in ${parent.name.namespace}.${parent.name.name} does not have the same type in ${type.name.namespace}.${type.name.name}`) |
| 54 | + process.exit(1) |
| 55 | + } |
| 56 | + |
| 57 | + // we have the same properties, so let's copy the metadata |
| 58 | + for (const key in property) { |
| 59 | + if (key === 'required') continue |
| 60 | + readProperty[key] = property[key] |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + return model |
| 68 | +} |
| 69 | + |
| 70 | +function deepEqual (a: any, b: any): boolean { |
| 71 | + try { |
| 72 | + assert.deepStrictEqual(a, b) |
| 73 | + return true |
| 74 | + } catch (err) { |
| 75 | + return false |
| 76 | + } |
| 77 | +} |
0 commit comments