Revision 0.34.48 (#6)

* Support Sparse Array Validation

* ChangeLog

* Version
This commit is contained in:
sinclairzx81
2026-01-23 17:59:38 +09:00
committed by GitHub
parent 3b0f4ea060
commit 11f18ac6e9
7 changed files with 39 additions and 6 deletions

View File

@@ -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)

4
package-lock.json generated
View File

@@ -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",

View File

@@ -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",

View File

@@ -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')

View File

@@ -117,8 +117,8 @@ function FromArray(schema: TArray, references: TSchema[], value: any): boolean {
if (IsDefined<number>(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 })())) {

View File

@@ -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)
})
})

View File

@@ -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))
})
})