From b00293ee1c173c78da03824668d68b9a2c769361 Mon Sep 17 00:00:00 2001 From: sinclairzx81 Date: Fri, 9 Aug 2024 11:13:27 +0900 Subject: [PATCH] Revision 0.33.1 (#945) * Interior Mutation Fix + Tests * Revision --- package-lock.json | 4 +-- package.json | 2 +- src/type/create/type.ts | 2 +- src/type/intrinsic/intrinsic.ts | 3 +- test/runtime/index.ts | 2 +- test/runtime/system/policy/index.ts | 1 + test/runtime/system/policy/instance-mode.ts | 34 +++++++++++++++++++++ test/runtime/value/check/not.ts | 9 ++---- 8 files changed, 45 insertions(+), 12 deletions(-) create mode 100644 test/runtime/system/policy/instance-mode.ts diff --git a/package-lock.json b/package-lock.json index 4b22dae..06a44d3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@sinclair/typebox", - "version": "0.33.0", + "version": "0.33.1", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@sinclair/typebox", - "version": "0.33.0", + "version": "0.33.1", "license": "MIT", "devDependencies": { "@arethetypeswrong/cli": "^0.13.2", diff --git a/package.json b/package.json index bfe38da..eb4c3e5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@sinclair/typebox", - "version": "0.33.0", + "version": "0.33.1", "description": "Json Schema Type Builder with Static Type Resolution for TypeScript", "keywords": [ "typescript", diff --git a/src/type/create/type.ts b/src/type/create/type.ts index 4d378a1..cb8371a 100644 --- a/src/type/create/type.ts +++ b/src/type/create/type.ts @@ -33,7 +33,7 @@ import { Clone } from '../clone/value' /** Creates TypeBox schematics using the configured InstanceMode */ export function CreateType(schema: Record, options?: SchemaOptions): unknown { - const result = options !== undefined ? Object.assign(options, schema) : schema + const result = options !== undefined ? { ...options, ...schema } : schema switch (TypeSystemPolicy.InstanceMode) { case 'freeze': return Immutable(result) diff --git a/src/type/intrinsic/intrinsic.ts b/src/type/intrinsic/intrinsic.ts index 6745597..bbb139b 100644 --- a/src/type/intrinsic/intrinsic.ts +++ b/src/type/intrinsic/intrinsic.ts @@ -130,6 +130,7 @@ export type TIntrinsic = T extends TUnion ? TUnion> : T extends TLiteral ? TLiteral> : T + /** Applies an intrinsic string manipulation to the given type. */ export function Intrinsic(schema: T, mode: M, options?: SchemaOptions): TIntrinsicFromMappedKey /** Applies an intrinsic string manipulation to the given type. */ @@ -141,7 +142,7 @@ export function Intrinsic(schema: TSchema, mode: IntrinsicMode, options: SchemaO // Intrinsic-Mapped-Inference IsMappedKey(schema) ? IntrinsicFromMappedKey(schema, mode, options) : // Standard-Inference - IsTemplateLiteral(schema) ? FromTemplateLiteral(schema, mode, schema) : + IsTemplateLiteral(schema) ? FromTemplateLiteral(schema, mode, options) : IsUnion(schema) ? Union(FromRest(schema.anyOf, mode), options) : IsLiteral(schema) ? Literal(FromLiteralValue(schema.const, mode), options) : // Default Type diff --git a/test/runtime/index.ts b/test/runtime/index.ts index 16011ae..15ca57a 100644 --- a/test/runtime/index.ts +++ b/test/runtime/index.ts @@ -3,7 +3,7 @@ import { TypeSystemPolicy } from '@sinclair/typebox/system' // ------------------------------------------------------------------ // InstanceMode: Freeze (Detect Unintended Side Effects) // ------------------------------------------------------------------ -TypeSystemPolicy.InstanceMode = 'default' +TypeSystemPolicy.InstanceMode = 'freeze' import './compiler/index' import './compiler-ajv/index' diff --git a/test/runtime/system/policy/index.ts b/test/runtime/system/policy/index.ts index 29bf296..144b64a 100644 --- a/test/runtime/system/policy/index.ts +++ b/test/runtime/system/policy/index.ts @@ -2,3 +2,4 @@ import './allow-array-object' import './allow-nan' import './allow-null-void' import './exact-optional-property-types' +import './instance-mode' diff --git a/test/runtime/system/policy/instance-mode.ts b/test/runtime/system/policy/instance-mode.ts new file mode 100644 index 0000000..1e400c2 --- /dev/null +++ b/test/runtime/system/policy/instance-mode.ts @@ -0,0 +1,34 @@ +import { TypeSystemPolicy } from '@sinclair/typebox/system' +import { Type } from '@sinclair/typebox' +import { Assert } from '../../assert/index' + +describe('system/TypeSystemPolicy/InstanceMode', () => { + after(() => { + TypeSystemPolicy.InstanceMode = 'freeze' + }) + // --------------------------------------------------------------- + // Number + // --------------------------------------------------------------- + it('Should use instance mode default', () => { + TypeSystemPolicy.InstanceMode = 'default' + const S = Type.String() + const T = Type.Array(S) + S.$id = 'updated' + Assert.IsEqual(T.items.$id, 'updated') + }) + it('Should use instance mode clone', () => { + TypeSystemPolicy.InstanceMode = 'clone' + const S = Type.String() + const T = Type.Array(S) + S.$id = 'updated' + Assert.IsEqual(T.items.$id, undefined) + }) + it('Should use instance mode freeze', () => { + TypeSystemPolicy.InstanceMode = 'freeze' + Assert.Throws(() => { + const S = Type.String() + const T = Type.Array(S) + S.$id = 'updated' + }) + }) +}) diff --git a/test/runtime/value/check/not.ts b/test/runtime/value/check/not.ts index ba5471b..866ec40 100644 --- a/test/runtime/value/check/not.ts +++ b/test/runtime/value/check/not.ts @@ -4,7 +4,7 @@ import { Assert } from '../../assert/index' describe('value/check/Not', () => { it('Should validate with not number', () => { - const T = Type.Not(Type.Number(), Type.String()) + const T = Type.Not(Type.Number()) Assert.IsEqual(Value.Check(T, 1), false) Assert.IsEqual(Value.Check(T, 'A'), true) }) @@ -14,7 +14,7 @@ describe('value/check/Not', () => { Type.Literal('A'), Type.Literal('B'), Type.Literal('C') - ]), Type.String()) + ])) Assert.IsEqual(Value.Check(T, 'A'), false) Assert.IsEqual(Value.Check(T, 'B'), false) Assert.IsEqual(Value.Check(T, 'C'), false) @@ -22,10 +22,7 @@ describe('value/check/Not', () => { }) it('Should validate with union right', () => { // prettier-ignore - const T = Type.Not(Type.Number(), Type.Union([ - Type.String(), - Type.Boolean() - ])) + const T = Type.Not(Type.Number()) Assert.IsEqual(Value.Check(T, 1), false) Assert.IsEqual(Value.Check(T, 'A'), true) Assert.IsEqual(Value.Check(T, true), true)