From f8e50286d779c171982dae65c4a9565b777edcf7 Mon Sep 17 00:00:00 2001 From: sinclairzx81 Date: Fri, 21 Apr 2023 21:43:21 +0900 Subject: [PATCH] Expression Variable Fix for Compiled Records (#403) --- package-lock.json | 4 ++-- package.json | 2 +- src/compiler/compiler.ts | 3 +-- test/runtime/compiler/record.ts | 17 +++++++++++++++++ 4 files changed, 21 insertions(+), 5 deletions(-) diff --git a/package-lock.json b/package-lock.json index 91b7e01..3da05bc 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@sinclair/typebox", - "version": "0.28.1", + "version": "0.28.2", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@sinclair/typebox", - "version": "0.28.1", + "version": "0.28.2", "license": "MIT", "devDependencies": { "@sinclair/hammer": "^0.17.1", diff --git a/package.json b/package.json index e8ff854..2ca7f90 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@sinclair/typebox", - "version": "0.28.1", + "version": "0.28.2", "description": "JSONSchema Type Builder with Static Type Resolution for TypeScript", "keywords": [ "typescript", diff --git a/src/compiler/compiler.ts b/src/compiler/compiler.ts index 02591e1..bf0d67a 100644 --- a/src/compiler/compiler.ts +++ b/src/compiler/compiler.ts @@ -248,7 +248,6 @@ export namespace TypeCompiler { if (IsNumber(schema.minimum)) yield `${value} >= ${schema.minimum}` if (IsNumber(schema.maximum)) yield `${value} <= ${schema.maximum}` } - function* Object(schema: Types.TObject, references: Types.TSchema[], value: string): IterableIterator { yield IsObjectCheck(value) if (IsNumber(schema.minProperties)) yield `Object.getOwnPropertyNames(${value}).length >= ${schema.minProperties}` @@ -288,7 +287,7 @@ export namespace TypeCompiler { if (IsNumber(schema.maxProperties)) yield `Object.getOwnPropertyNames(${value}).length <= ${schema.maxProperties}` const [patternKey, patternSchema] = globalThis.Object.entries(schema.patternProperties)[0] const local = PushLocal(`new RegExp(/${patternKey}/)`) - const check1 = CreateExpression(patternSchema, references, value) + const check1 = CreateExpression(patternSchema, references, 'value') const check2 = Types.TypeGuard.TSchema(schema.additionalProperties) ? CreateExpression(schema.additionalProperties, references, value) : schema.additionalProperties === false ? 'false' : 'true' const expression = `(${local}.test(key) ? ${check1} : ${check2})` yield `(Object.entries(${value}).every(([key, value]) => ${expression}))` diff --git a/test/runtime/compiler/record.ts b/test/runtime/compiler/record.ts index 2f39b56..8bee7fa 100644 --- a/test/runtime/compiler/record.ts +++ b/test/runtime/compiler/record.ts @@ -2,6 +2,23 @@ import { Type } from '@sinclair/typebox' import { Ok, Fail } from './validate' describe('type/compiler/Record', () => { + // ------------------------------------------------------------- + // Issues + // ------------------------------------------------------------- + it('Issue: https://github.com/sinclairzx81/typebox/issues/402', () => { + const T = Type.Object({ + foo: Type.Object({ + bar: Type.Record(Type.String(), Type.Number()), + }), + }) + Ok(T, { foo: { bar: { x: 42 } } }) + Ok(T, { foo: { bar: {} } }) + Fail(T, { foo: { bar: { x: '42' } } }) + Fail(T, { foo: { bar: [] } }) + Fail(T, { foo: {} }) + Fail(T, { foo: [] }) + Fail(T, {}) + }) // ------------------------------------------------------------- // TypeBox Only: Date and Record // -------------------------------------------------------------