|
| 1 | +/** |
| 2 | + * Copyright 2025 IBM Corporation. |
| 3 | + * SPDX-License-Identifier: Apache2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +const { validate } = require('jsonschema'); |
| 7 | +const { validateSubschemas } = require('@ibm-cloud/openapi-ruleset-utilities'); |
| 8 | +const { LoggerFactory } = require('../utils'); |
| 9 | + |
| 10 | +let ruleId; |
| 11 | +let logger; |
| 12 | + |
| 13 | +module.exports = function (schema, _opts, context) { |
| 14 | + if (!logger) { |
| 15 | + ruleId = context.rule.name; |
| 16 | + logger = LoggerFactory.getInstance().getLogger(ruleId); |
| 17 | + } |
| 18 | + |
| 19 | + return validateSubschemas(schema, context.path, checkSchemaExamples); |
| 20 | +}; |
| 21 | + |
| 22 | +function checkSchemaExamples(schema, path) { |
| 23 | + if (!isDefined(schema.example) && !definesElements(schema.examples)) { |
| 24 | + return []; |
| 25 | + } |
| 26 | + |
| 27 | + const examplesToCheck = []; |
| 28 | + |
| 29 | + if (definesElements(schema.examples)) { |
| 30 | + schema.examples.forEach((example, i) => { |
| 31 | + examplesToCheck.push({ |
| 32 | + schema, |
| 33 | + example, |
| 34 | + path: [...path, 'examples', i], |
| 35 | + }); |
| 36 | + }); |
| 37 | + } |
| 38 | + |
| 39 | + if (isDefined(schema.example)) { |
| 40 | + examplesToCheck.push({ |
| 41 | + schema, |
| 42 | + example: schema.example, |
| 43 | + path: [...path, 'example'], |
| 44 | + }); |
| 45 | + } |
| 46 | + |
| 47 | + return validateExamples(examplesToCheck); |
| 48 | +} |
| 49 | + |
| 50 | +function validateExamples(examples) { |
| 51 | + return examples |
| 52 | + .map(({ schema, example, path }) => { |
| 53 | + // Setting required: true prevents undefined values from passing validation. |
| 54 | + const { valid, errors } = validate(example, schema, { required: true }); |
| 55 | + if (!valid) { |
| 56 | + const message = getMessage(errors, example, schema); |
| 57 | + return { |
| 58 | + message: `Schema example is not valid: ${message}`, |
| 59 | + path, |
| 60 | + }; |
| 61 | + } |
| 62 | + }) |
| 63 | + .filter(e => isDefined(e)); |
| 64 | +} |
| 65 | + |
| 66 | +function isDefined(x) { |
| 67 | + return x !== undefined; |
| 68 | +} |
| 69 | + |
| 70 | +function definesElements(arr) { |
| 71 | + return Array.isArray(arr) && arr.length; |
| 72 | +} |
| 73 | + |
| 74 | +function getMessage(errors, example, schema) { |
| 75 | + let message = getPrimaryErrorMessage(errors); |
| 76 | + |
| 77 | + const primaryError = errors[0]; |
| 78 | + if (Array.isArray(schema.oneOf) || Array.isArray(schema.anyOf)) { |
| 79 | + // If a schema has a oneOf or anyOf, jsonschema will supress nested validation |
| 80 | + // error messages by default. If this is the case, compute those messages and |
| 81 | + // append them to the primary message (on their own, they don't include context |
| 82 | + // and thus wouldn't be very helpful). |
| 83 | + const { errors } = validate(example, schema, { nestedErrors: true }); |
| 84 | + message = appendMessage(message, getPrimaryErrorMessage(errors)); |
| 85 | + } else if (Array.isArray(primaryError.argument?.valid?.errors)) { |
| 86 | + // Sometimes, jsonschema buries additional error info in the 'argument' |
| 87 | + // field of the validation result. If so, extract and include it. |
| 88 | + message = appendMessage( |
| 89 | + message, |
| 90 | + getPrimaryErrorMessage(primaryError.argument.valid.errors) |
| 91 | + ); |
| 92 | + } |
| 93 | + |
| 94 | + return message; |
| 95 | +} |
| 96 | + |
| 97 | +function getPrimaryErrorMessage(errors) { |
| 98 | + const { path } = errors[0]; |
| 99 | + let { message } = errors[0]; |
| 100 | + |
| 101 | + // If the violation is nested within an object or array, this field will hold |
| 102 | + // the path segments to the violation, which is necessary context for the user. |
| 103 | + if (path.length) { |
| 104 | + message = `${path.join('.')} ${message}`; |
| 105 | + } |
| 106 | + |
| 107 | + // Sometimes, jsonschema appends a confusing message to an error - remove it. |
| 108 | + return message.replace(/ with \d+ error\[s\]:$/, ''); |
| 109 | +} |
| 110 | + |
| 111 | +function appendMessage(msg, app) { |
| 112 | + return `${msg} (${app})`; |
| 113 | +} |
0 commit comments