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