diff --git a/packages/schema/package.json b/packages/schema/package.json index f5e9a632..d83df81f 100644 --- a/packages/schema/package.json +++ b/packages/schema/package.json @@ -1,7 +1,7 @@ { "name": "@apimatic/schema", "author": "APIMatic Ltd.", - "version": "0.7.4", + "version": "0.7.5", "license": "MIT", "sideEffects": false, "main": "lib/index.js", diff --git a/packages/schema/src/types/oneOf.ts b/packages/schema/src/types/oneOf.ts index 3d1b51ac..858430e2 100644 --- a/packages/schema/src/types/oneOf.ts +++ b/packages/schema/src/types/oneOf.ts @@ -191,6 +191,8 @@ function matchAndMap>>( ctxt: SchemaContextCreator ) { const matchedSchemas: Array> = []; + ctxt.strictValidation = true; + for (const schema of schemas) { if (schema.validateBeforeMap(value, ctxt).length === 0) { matchedSchemas.push(schema); @@ -207,6 +209,8 @@ function matchAndUnmap>>( ctxt: SchemaContextCreator ) { const matchedSchemas: Array> = []; + ctxt.strictValidation = true; + for (const schema of schemas) { if (schema.validateBeforeUnmap(value, ctxt).length === 0) { matchedSchemas.push(schema); @@ -223,6 +227,8 @@ function matchAndMapXml>>( ctxt: SchemaContextCreator ) { const matchedSchemas: Array> = []; + ctxt.strictValidation = true; + for (const schema of schemas) { if (schema.validateBeforeMapXml(value, ctxt).length === 0) { matchedSchemas.push(schema); @@ -239,6 +245,8 @@ function matchAndUnmapXml>>( ctxt: SchemaContextCreator ) { const matchedSchemas: Array> = []; + ctxt.strictValidation = true; + for (const schema of schemas) { if (schema.validateBeforeMapXml(value, ctxt).length === 0) { matchedSchemas.push(schema); diff --git a/packages/schema/test/types.ts b/packages/schema/test/types.ts index d717996e..64936e8a 100644 --- a/packages/schema/test/types.ts +++ b/packages/schema/test/types.ts @@ -1,5 +1,5 @@ import { Schema } from '../lib/schema'; -import { object } from '../src'; +import { bigint, boolean, object, oneOf, optional } from '../src'; import { string } from '../src/types/string'; import { number } from '../src/types/number'; import { validateAndMap } from '../src/schema'; @@ -17,11 +17,15 @@ export const animalSchema: Schema = object({ export interface Human { name: string; age: number; + id?: bigint | string; + hasChildren?: boolean | string; } export const humanSchema: Schema = object({ name: ['name', string()], age: ['age', number()], + id: ['id', optional(oneOf([string(), bigint()]))], + hasChildren: ['hasChildren', optional(oneOf([string(), boolean()]))], }); export function isHuman(value: unknown): value is Human { @@ -44,13 +48,13 @@ export const addressSchema: Schema
= object({ export interface Person { name: string; - age: number; + age: number | string; address: Address; } export const personSchema: Schema = object({ name: ['name', string()], - age: ['age', number()], + age: ['age', oneOf([string(), number()])], address: ['address', lazy(() => addressSchema)], }); diff --git a/packages/schema/test/types/oneOf.test.ts b/packages/schema/test/types/oneOf.test.ts index e59b01a3..69dd0a3a 100644 --- a/packages/schema/test/types/oneOf.test.ts +++ b/packages/schema/test/types/oneOf.test.ts @@ -204,10 +204,10 @@ describe('OnyOf', () => { expect((output as any).result).toBeUndefined(); }); - it('should handle oneOf with deep nesting', () => { + it('should handle oneOf with deep nesting of oneof type between string|number', () => { const input: Person | Address = { name: 'John', - age: 30, + age: '30', address: { street: '123 Main St', city: 'New York', @@ -219,6 +219,19 @@ describe('OnyOf', () => { expect(output.errors).toBeFalsy(); expect((output as any).result).toStrictEqual(input); }); + it('should handle oneOf with deep nesting of oneof type between string|bigint and string|boolean', () => { + const input: Human | Animal = { + name: 'John', + age: 30, + id: '9532532599932222222', + hasChildren: 'true', + }; + + const schema = oneOf([humanSchema, animalSchema]); + const output = validateAndMap(input, schema); + expect(output.errors).toBeFalsy(); + expect((output as any).result).toStrictEqual(input); + }); it('should map oneOf with discriminator', () => { const schema1 = object({