Revision 0.33.1 (#945)

* Interior Mutation Fix + Tests

* Revision
This commit is contained in:
sinclairzx81
2024-08-09 11:13:27 +09:00
committed by GitHub
parent 333c2a149e
commit b00293ee1c
8 changed files with 45 additions and 12 deletions
+2 -2
View File
@@ -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",
+1 -1
View File
@@ -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",
+1 -1
View File
@@ -33,7 +33,7 @@ import { Clone } from '../clone/value'
/** Creates TypeBox schematics using the configured InstanceMode */
export function CreateType(schema: Record<any, unknown>, 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)
+2 -1
View File
@@ -130,6 +130,7 @@ export type TIntrinsic<T extends TSchema, M extends IntrinsicMode> =
T extends TUnion<infer S> ? TUnion<TFromRest<S, M>> :
T extends TLiteral<infer S> ? TLiteral<TFromLiteralValue<S, M>> :
T
/** Applies an intrinsic string manipulation to the given type. */
export function Intrinsic<T extends TMappedKey, M extends IntrinsicMode>(schema: T, mode: M, options?: SchemaOptions): TIntrinsicFromMappedKey<T, M>
/** 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
+1 -1
View File
@@ -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'
+1
View File
@@ -2,3 +2,4 @@ import './allow-array-object'
import './allow-nan'
import './allow-null-void'
import './exact-optional-property-types'
import './instance-mode'
@@ -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'
})
})
})
+3 -6
View File
@@ -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)