-
Notifications
You must be signed in to change notification settings - Fork 205
Expand file tree
/
Copy pathno-invalid-schema-examples.ts
More file actions
40 lines (36 loc) · 1.26 KB
/
no-invalid-schema-examples.ts
File metadata and controls
40 lines (36 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import { getAdditionalPropertiesOption, validateExample } from '../utils';
import type { UserContext } from '../../walk';
import type { Oas3_1Schema, Oas3Schema } from '../../typings/openapi';
import type { Oas2Rule, Oas3Rule } from '../../visitors';
export const NoInvalidSchemaExamples: Oas3Rule | Oas2Rule = (opts: any) => {
const allowAdditionalProperties = getAdditionalPropertiesOption(opts) ?? false;
return {
Schema: {
leave(schema: Oas3_1Schema | Oas3Schema, ctx: UserContext) {
const examples = (schema as Oas3_1Schema).examples;
if (examples) {
for (const example of examples) {
validateExample(
example,
schema,
ctx.location.child(['examples', examples.indexOf(example)]),
ctx,
allowAdditionalProperties
);
}
}
if (schema.example !== undefined) {
// Handle nullable example for OAS3
if (
(schema as Oas3Schema).nullable === true &&
schema.example === null &&
schema.type !== undefined
) {
return;
}
validateExample(schema.example, schema, ctx.location.child('example'), ctx, true);
}
},
},
};
};