UNPKG

@redocly/openapi-core

Version:

See https://github.com/Redocly/redocly-cli

99 lines 3.56 kB
import * as redoclyConfig from '@redocly/config'; import { dequal } from './dequal.js'; import { isPlainObject } from './is-plain-object.js'; function transformEntityTypeName(subjectType, entityType) { const capitalizedEntityType = entityType .split('-') .map((part) => part.charAt(0).toUpperCase() + part.slice(1)) .join(''); const specificType = capitalizedEntityType + subjectType; if (Object.values(redoclyConfig.ENTITY_NODE_TYPE_NAMES).includes(specificType)) { return specificType; } return subjectType; } export function transformScorecardRulesToAssertions(entityType, rules) { const assertionConfig = {}; for (const [ruleKey, ruleValue] of Object.entries(rules)) { if (isAssertionRule(ruleKey, ruleValue)) { if (ruleValue.severity === 'off') { continue; } assertionConfig[ruleKey] = { ...buildAssertionWithNormalizedTypes(entityType, ruleKey, ruleValue), }; } else { assertionConfig[ruleKey] = ruleValue; } } return assertionConfig; } export function categorizeAssertions(assertionConfig) { const entityRules = {}; const apiRules = {}; for (const [ruleKey, ruleValue] of Object.entries(assertionConfig)) { if (isAssertionRule(ruleKey, ruleValue)) { const assertion = ruleValue; if (isEntityAssertion(assertion)) { entityRules[ruleKey] = assertion; } else { apiRules[ruleKey] = assertion; } } else { apiRules[ruleKey] = ruleValue; } } return { entityRules, apiRules }; } export function findDataSchemaInDocument(schemaKey, schemaJson, document) { if (!isPlainObject(document.parsed) || !isPlainObject(document.parsed.components)) { return null; } const components = document.parsed.components; const schemas = 'schemas' in components ? components.schemas : undefined; if (!schemas || !(schemaKey in schemas)) { return null; } const foundSchema = schemas[schemaKey]; try { const expectedSchema = JSON.parse(schemaJson); if (dequal(foundSchema, expectedSchema)) { return foundSchema; } } catch { return null; } return null; } function isAssertionRule(ruleKey, ruleValue) { return ruleKey.startsWith('rule/') && isPlainObject(ruleValue); } function isEntityAssertion(assertion) { return Object.values(redoclyConfig.ENTITY_NODE_TYPE_NAMES).some((entityTypeName) => assertion.subject.type === entityTypeName); } function buildAssertionWithNormalizedTypes(entityType, ruleKey, rawAssertion) { const transformedSubjectType = transformEntityTypeName(rawAssertion.subject.type, entityType); const transformedWhere = rawAssertion.where?.map((whereClause) => ({ ...whereClause, subject: { ...whereClause.subject, type: transformEntityTypeName(whereClause.subject.type, entityType), }, })); return { assertionId: ruleKey, subject: { ...rawAssertion.subject, type: transformedSubjectType, }, assertions: rawAssertion.assertions, ...(transformedWhere && { where: transformedWhere }), ...(rawAssertion.message && { message: rawAssertion.message }), ...(rawAssertion.severity && { severity: rawAssertion.severity }), }; } //# sourceMappingURL=scorecards.js.map