Skip to content

Commit 28116f8

Browse files
Devesh0129Logende
andauthored
2 new files added (#942)
* 2 new files added * apply formatting changes * update to use reverse strategy * add test case * do not remove content from sub-schema when it is changed to $ref in schema diagram * apply formatting changes --------- Co-authored-by: Devesh0129 <Devesh0129@users.noreply.github.com> Co-authored-by: Felix Neubauer <felix@neuby.de> Co-authored-by: Logende <Logende@users.noreply.github.com>
1 parent a9225ab commit 28116f8

4 files changed

Lines changed: 128 additions & 0 deletions

File tree

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import {describe, expect, it, vi} from 'vitest';
2+
import {type JsonSchemaType} from '../jsonSchemaType';
3+
import {cleanupSchemaByType} from '@/schema/cleanupSchemaByType.ts';
4+
5+
describe('test cleanupSchemaByType', () => {
6+
const schema: any = {
7+
required: ['a'],
8+
properties: {
9+
obj: {
10+
type: 'object',
11+
additionalProperties: false,
12+
properties: {
13+
name: {
14+
type: 'string',
15+
},
16+
},
17+
},
18+
str: {
19+
type: 'string',
20+
minLength: 5,
21+
pattern: '^[a-zA-Z]+$',
22+
},
23+
num: {
24+
type: 'number',
25+
minimum: 0,
26+
maximum: 100,
27+
},
28+
arr: {
29+
type: 'array',
30+
items: {
31+
type: 'string',
32+
},
33+
minItems: 1,
34+
},
35+
},
36+
} as JsonSchemaType;
37+
38+
it('test changing obj to something else removes the object constraints', () => {
39+
// initial object schema should have object-specific constraints
40+
const objectSchema = schema.properties.obj;
41+
expect(objectSchema).toHaveProperty('additionalProperties');
42+
expect(objectSchema).toHaveProperty('properties');
43+
44+
// update object schema to be a string schema instead
45+
objectSchema.type = 'string';
46+
47+
// run the cleanup function
48+
cleanupSchemaByType(objectSchema, 'string');
49+
50+
// object-specific constraints should be removed
51+
expect(objectSchema).not.toHaveProperty('additionalProperties');
52+
expect(objectSchema).not.toHaveProperty('properties');
53+
});
54+
55+
it('test changing string to something else removes the string constraints', () => {
56+
// initial string schema should have string-specific constraints
57+
const stringSchema = schema.properties.str;
58+
expect(stringSchema).toHaveProperty('minLength');
59+
expect(stringSchema).toHaveProperty('pattern');
60+
61+
// update string schema to be a number schema instead
62+
stringSchema.type = 'number';
63+
64+
// run the cleanup function
65+
cleanupSchemaByType(stringSchema, 'number');
66+
67+
// string-specific constraints should be removed
68+
expect(stringSchema).not.toHaveProperty('minLength');
69+
expect(stringSchema).not.toHaveProperty('pattern');
70+
});
71+
72+
it('test changing number to something else removes the number constraints', () => {
73+
// initial number schema should have number-specific constraints
74+
const numberSchema = schema.properties.num;
75+
expect(numberSchema).toHaveProperty('minimum');
76+
expect(numberSchema).toHaveProperty('maximum');
77+
78+
// update number schema to be an array schema instead
79+
numberSchema.type = 'array';
80+
81+
// run the cleanup function
82+
cleanupSchemaByType(numberSchema, 'array');
83+
84+
// number-specific constraints should be removed
85+
expect(numberSchema).not.toHaveProperty('minimum');
86+
expect(numberSchema).not.toHaveProperty('maximum');
87+
});
88+
});
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import {CONSTRAINED_KEYWORDS} from './supportedJsonSchemaTypeKeywords';
2+
import type {SchemaPropertyType} from '@/schema/jsonSchemaType.ts';
3+
4+
export function cleanupSchemaByType(schema: Record<string, any>, type: SchemaPropertyType) {
5+
Object.keys(schema).forEach(key => {
6+
if (key in CONSTRAINED_KEYWORDS) {
7+
const supportedTypesForProperty = CONSTRAINED_KEYWORDS[key]!;
8+
if (!supportedTypesForProperty.includes(type)) {
9+
delete schema[key];
10+
}
11+
}
12+
});
13+
}

meta_configurator/src/schema/graph-representation/typeUtils.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type {JsonSchemaObjectType, SchemaPropertyTypes} from '@/schema/jsonSchem
22
import {pathToJsonPointer} from '@/utility/pathUtils';
33
import {SchemaNodeData} from '@/schema/graph-representation/schemaGraphTypes';
44
import type {Path} from '@/utility/path';
5+
import {cleanupSchemaByType} from '../cleanupSchemaByType';
56

67
export type AttributeTypeChoice = {label: string; schema: JsonSchemaObjectType};
78

@@ -111,6 +112,7 @@ export function applyNewType(
111112
) {
112113
if (typeSchema.type !== undefined) {
113114
currentSchema.type = typeSchema.type;
115+
cleanupSchemaByType(currentSchema, typeSchema.type);
114116
if (typeSchema.type === 'array') {
115117
if (
116118
currentSchema.items === undefined ||
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import type {SchemaPropertyType} from '@/schema/jsonSchemaType.ts';
2+
3+
export const CONSTRAINED_KEYWORDS: Record<string, SchemaPropertyType[]> = {
4+
minLength: ['string'],
5+
maxLength: ['string'],
6+
pattern: ['string'],
7+
format: ['string'],
8+
minimum: ['number', 'integer'],
9+
maximum: ['number', 'integer'],
10+
exclusiveMinimum: ['number', 'integer'],
11+
exclusiveMaximum: ['number', 'integer'],
12+
multipleOf: ['number', 'integer'],
13+
properties: ['object'],
14+
patternProperties: ['object'],
15+
required: ['object'],
16+
additionalProperties: ['object'],
17+
propertyNames: ['object'],
18+
items: ['array'],
19+
minItems: ['array'],
20+
maxItems: ['array'],
21+
uniqueItems: ['array'],
22+
contains: ['array'],
23+
maxContains: ['array'],
24+
minContains: ['array'],
25+
};

0 commit comments

Comments
 (0)