persona-community-5/.pnpm-store/v3/files/d7/d62a56e6637f14145fdd0690b85b62bc57097c127179eaf559d1c4cef748c7bb6412580b870390456474c5d31373ae4aa60ba1ba57d249b8d31f615d992e81
rdev-worker a1d0d1bf1c
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed
build: /implement-feature community-ui --requirements 'Build the React commu...
2026-02-24 08:22:30 +00:00

60 lines
1.8 KiB
Plaintext

import { SpecVersion } from '../../oas-types';
import type { Oas2Rule, Oas3Rule } from '../../visitors';
import type { UserContext } from '../../walk';
import type { Oas2Schema } from '../../typings/swagger';
import type { Oas3Schema, Oas3_1Schema } from '../../typings/openapi';
const SCALAR_TYPES = ['string', 'integer', 'number', 'boolean', 'null'];
export const ScalarPropertyMissingExample: Oas3Rule | Oas2Rule = () => {
return {
SchemaProperties(
properties: { [name: string]: Oas2Schema | Oas3Schema | Oas3_1Schema },
{ report, location, oasVersion, resolve }: UserContext
) {
for (const propName of Object.keys(properties)) {
const propSchema = resolve(properties[propName]).node;
if (!propSchema || !isScalarSchema(propSchema)) {
continue;
}
if (
propSchema.example === undefined &&
(propSchema as Oas3_1Schema).examples === undefined
) {
report({
message: `Scalar property should have "example"${
oasVersion === SpecVersion.OAS3_1 ? ' or "examples"' : ''
} defined.`,
location: location.child(propName).key(),
});
}
}
},
};
};
function isScalarSchema(schema: Oas2Schema | Oas3Schema | Oas3_1Schema) {
if (!schema.type) {
return false;
}
if (schema.allOf || (schema as Oas3Schema).anyOf || (schema as Oas3Schema).oneOf) {
// Skip allOf/oneOf/anyOf as it's complicated to validate it right now.
// We need core support for checking contrstrains through those keywords.
return false;
}
if (schema.format === 'binary') {
return false;
}
if (Array.isArray(schema.type)) {
return schema.type.every((t) => SCALAR_TYPES.includes(t));
}
return SCALAR_TYPES.includes(schema.type);
}