@rhoas/spectral-ruleset
Version:
60 lines • 2.4 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
// Recursively compare the target value of the OpenAPI document with an expected value
exports.default = (targetVal, { definition }, context) => {
const path = [...context.path];
const targetSchema = path[path.length - 1];
if (!targetVal) {
return [
{
message: 'Missing required schema object "' + targetSchema + '"',
path
}
];
}
return compareSchemas(definition, targetVal, path);
};
function compareSchemas(expectedSchema, actualSchema, path = []) {
const { type, properties, required } = expectedSchema;
const results = [];
if (required && (!actualSchema || Object.keys(actualSchema).length === 0)) {
results.push({
message: '`schema` object is required and should be non-empty',
path
});
}
if ((actualSchema === null || actualSchema === void 0 ? void 0 : actualSchema.type) != type) {
results.push({
message: `"type" should be a "${type}", got "${actualSchema.type}"`,
path: [...path, 'type']
});
}
if (!actualSchema.properties) {
results.push({
message: 'missing required "properties" object',
path: [...path, 'properties']
});
return results;
}
for (const [propertyName, propertyConfig] of Object.entries(properties)) {
const schemaProperty = Object.keys(actualSchema.properties).find((p => p === propertyName));
if (!schemaProperty) {
if (propertyConfig.required) {
results.push({
message: `missing required property "${propertyName}"`,
path: [...path, 'properties']
});
}
continue;
}
const schemaPropertyConfig = actualSchema.properties[schemaProperty];
if ((schemaPropertyConfig === null || schemaPropertyConfig === void 0 ? void 0 : schemaPropertyConfig.type) !== propertyConfig.type) {
results.push({
message: `"type" for "${propertyName}" should be of type "${propertyConfig.type}" but got "${schemaPropertyConfig.type}"`,
path: [...path, 'properties', propertyName]
});
}
}
return results;
}
//# sourceMappingURL=schemaDefinition.js.map