diff --git a/changelog/0.34.0.md b/changelog/0.34.0.md index 15b506f..55fa460 100644 --- a/changelog/0.34.0.md +++ b/changelog/0.34.0.md @@ -3,6 +3,8 @@ --- ### Revision Updates +- [Revision 0.34.48](https://github.com/sinclairzx81/typebox-legacy/pull/6) + - Support Sparse Array Validation - [Revision 0.34.47](https://github.com/sinclairzx81/typebox-legacy/pull/5) - Annual License Update - [Revision 0.34.46](https://github.com/sinclairzx81/typebox-legacy/pull/4) diff --git a/package-lock.json b/package-lock.json index 06b0790..a2a296a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@sinclair/typebox", - "version": "0.34.47", + "version": "0.34.48", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@sinclair/typebox", - "version": "0.34.47", + "version": "0.34.48", "license": "MIT", "devDependencies": { "@arethetypeswrong/cli": "^0.13.2", diff --git a/package.json b/package.json index 313160b..db8fd39 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@sinclair/typebox", - "version": "0.34.47", + "version": "0.34.48", "description": "Json Schema Type Builder with Static Type Resolution for TypeScript", "keywords": [ "typescript", diff --git a/src/compiler/compiler.ts b/src/compiler/compiler.ts index 84ba3c8..3c61539 100644 --- a/src/compiler/compiler.ts +++ b/src/compiler/compiler.ts @@ -257,7 +257,8 @@ export namespace TypeCompiler { if (IsNumber(schema.maxItems)) yield `${value}.length <= ${schema.maxItems}` if (IsNumber(schema.minItems)) yield `${value}.length >= ${schema.minItems}` const elementExpression = CreateExpression(schema.items, references, 'value') - yield `${value}.every((${parameter}) => ${elementExpression})` + // yield `${value}.every((${parameter}) => ${elementExpression})` // issue: 1519 + yield `((array) => { for(const ${parameter} of array) if(!(${elementExpression})) { return false }; return true; })(${value})` if (IsSchema(schema.contains) || IsNumber(schema.minContains) || IsNumber(schema.maxContains)) { const containsSchema = IsSchema(schema.contains) ? schema.contains : Never() const checkExpression = CreateExpression(containsSchema, references, 'value') diff --git a/src/value/check/check.ts b/src/value/check/check.ts index c1271ce..69fb024 100644 --- a/src/value/check/check.ts +++ b/src/value/check/check.ts @@ -117,8 +117,8 @@ function FromArray(schema: TArray, references: TSchema[], value: any): boolean { if (IsDefined(schema.maxItems) && !(value.length <= schema.maxItems)) { return false } - if (!value.every((value) => Visit(schema.items, references, value))) { - return false + for (const element of value) { + if (!Visit(schema.items, references, element)) return false } // prettier-ignore if (schema.uniqueItems === true && !((function() { const set = new Set(); for(const element of value) { const hashed = Hash(element); if(set.has(hashed)) { return false } else { set.add(hashed) } } return true })())) { diff --git a/test/runtime/compiler/array.ts b/test/runtime/compiler/array.ts index 70af4c5..b6a0d42 100644 --- a/test/runtime/compiler/array.ts +++ b/test/runtime/compiler/array.ts @@ -183,4 +183,19 @@ describe('compiler/Array', () => { ], }) }) + // ---------------------------------------------------------------- + // Issue: https://github.com/sinclairzx81/typebox/issues/1519 + // ---------------------------------------------------------------- + it('Should correctly handle sparse arrays 1', () => { + const T = Type.Array(Type.String()) + const V = [] + V[10] = 'hello' + Fail(T, V) + }) + it('Should correctly handle sparse arrays 2', () => { + const T = Type.Array(Type.Union([Type.String(), Type.Undefined()])) + const V = [] + V[10] = 'hello' + Ok(T, V) + }) }) diff --git a/test/runtime/value/check/array.ts b/test/runtime/value/check/array.ts index 9ad1be5..ee81e41 100644 --- a/test/runtime/value/check/array.ts +++ b/test/runtime/value/check/array.ts @@ -152,4 +152,19 @@ describe('value/check/Array', () => { }), ) }) + // ---------------------------------------------------------------- + // Issue: https://github.com/sinclairzx81/typebox/issues/1519 + // ---------------------------------------------------------------- + it('Should correctly handle sparse arrays 1', () => { + const T = Type.Array(Type.String()) + const V = [] + V[10] = 'hello' + Assert.IsFalse(Value.Check(T, V)) + }) + it('Should correctly handle sparse arrays 2', () => { + const T = Type.Array(Type.Union([Type.String(), Type.Undefined()])) + const V = [] + V[10] = 'hello' + Assert.IsTrue(Value.Check(T, V)) + }) })