Skip to content

Commit 5a872b6

Browse files
committed
update to use reverse strategy
1 parent 19d6744 commit 5a872b6

File tree

2 files changed

+32
-48
lines changed

2 files changed

+32
-48
lines changed
Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
1-
import {SUPPORTED_KEYWORDS} from './supportedJsonSchemaTypeKeywords';
2-
import type {JsonSchemaType} from './supportedJsonSchemaTypeKeywords';
3-
export function cleanupSchemaByType(schema: Record<string, any>, type: JsonSchemaType | '$ref') {
4-
let allowed: string[];
5-
6-
if (type === '$ref') {
7-
allowed = ['$ref', 'title', 'description', 'default', 'examples'];
8-
} else {
9-
allowed = SUPPORTED_KEYWORDS[type];
10-
}
1+
import {CONSTRAINED_KEYWORDS} from './supportedJsonSchemaTypeKeywords';
2+
import type {SchemaPropertyType} from '@/schema/jsonSchemaType.ts';
113

4+
export function cleanupSchemaByType(schema: Record<string, any>, type: SchemaPropertyType) {
125
Object.keys(schema).forEach(key => {
13-
if (!allowed.includes(key) && key !== 'type') {
14-
delete schema[key];
6+
if (key in CONSTRAINED_KEYWORDS) {
7+
const supportedTypesForProperty = CONSTRAINED_KEYWORDS[key]!;
8+
if (!supportedTypesForProperty.includes(type)) {
9+
delete schema[key];
10+
}
1511
}
1612
});
1713
}
Lines changed: 24 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,26 @@
1-
export type JsonSchemaType =
2-
| 'string'
3-
| 'number'
4-
| 'integer'
5-
| 'object'
6-
| 'array'
7-
| 'boolean'
8-
| 'null';
1+
import type {SchemaPropertyType} from '@/schema/jsonSchemaType.ts';
92

10-
const COMMON = ['title', 'description', 'default', 'examples', 'enum', 'const'];
113

12-
const GENERIC = ['allOf', 'anyOf', 'oneOf', 'not'];
13-
14-
export const SUPPORTED_KEYWORDS: Record<JsonSchemaType, string[]> = {
15-
string: [...COMMON, ...GENERIC, 'minLength', 'maxLength', 'pattern', 'format'],
16-
number: [
17-
...COMMON,
18-
...GENERIC,
19-
'minimum',
20-
'maximum',
21-
'exclusiveMinimum',
22-
'exclusiveMaximum',
23-
'multipleOf',
24-
],
25-
integer: [
26-
...COMMON,
27-
...GENERIC,
28-
'minimum',
29-
'maximum',
30-
'exclusiveMinimum',
31-
'exclusiveMaximum',
32-
'multipleOf',
33-
],
34-
object: [...COMMON, ...GENERIC, 'properties', 'required', 'additionalProperties'],
35-
array: [...COMMON, ...GENERIC, 'items', 'minItems', 'maxItems', 'uniqueItems'],
36-
boolean: [...COMMON, ...GENERIC],
37-
null: [...COMMON, ...GENERIC],
38-
};
4+
export const CONSTRAINED_KEYWORDS: Record<string, SchemaPropertyType[]> = {
5+
minLength: ['string'],
6+
maxLength: ['string'],
7+
pattern: ['string'],
8+
format: ['string'],
9+
minimum: ['number', 'integer'],
10+
maximum: ['number', 'integer'],
11+
exclusiveMinimum: ['number', 'integer'],
12+
exclusiveMaximum: ['number', 'integer'],
13+
multipleOf: ['number', 'integer'],
14+
properties: ['object'],
15+
patternProperties: ['object'],
16+
required: ['object'],
17+
additionalProperties: ['object'],
18+
propertyNames: ['object'],
19+
items: ['array'],
20+
minItems: ['array'],
21+
maxItems: ['array'],
22+
uniqueItems: ['array'],
23+
contains: ['array'],
24+
maxContains: ['array'],
25+
minContains: ['array'],
26+
}

0 commit comments

Comments
 (0)