Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revision 0.34.3 #1083

Merged
merged 3 commits into from
Nov 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions changelog/0.34.0.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
### 0.34.0
- [Revision 0.34.1](https://github.com/sinclairzx81/typebox/pull/1080)
- Implement Computed Type Deref in Modules

- [Revision 0.34.3](https://github.com/sinclairzx81/typebox/pull/1083)
- Retain Array Elements on Value Default
- [Revision 0.34.2](https://github.com/sinclairzx81/typebox/pull/1082)
- Resolve import pathing issue introduced on 0.34.1
- [Revision 0.34.1](https://github.com/sinclairzx81/typebox/pull/1080)
- Implement Computed Type Deref in Modules

## [0.34.0](https://www.npmjs.com/package/@sinclair/typebox/v/0.34.0)

Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@sinclair/typebox",
"version": "0.34.2",
"version": "0.34.3",
"description": "Json Schema Type Builder with Static Type Resolution for TypeScript",
"keywords": [
"typescript",
Expand Down
8 changes: 8 additions & 0 deletions src/value/default/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ function HasDefaultProperty(schema: unknown): schema is TSchema {
// Types
// ------------------------------------------------------------------
function FromArray(schema: TArray, references: TSchema[], value: unknown): any {
// if the value is an array, we attempt to initialize it's elements
if (IsArray(value)) {
for (let i = 0; i < value.length; i++) {
value[i] = Visit(schema.items, references, value[i])
}
return value
}
// ... otherwise use default initialization
const defaulted = ValueOrDefault(schema, value)
if (!IsArray(defaulted)) return defaulted
for (let i = 0; i < defaulted.length; i++) {
Expand Down
26 changes: 26 additions & 0 deletions test/runtime/value/default/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,30 @@ describe('value/default/Array', () => {
const R = Value.Default(T, [1, undefined, 3])
Assert.IsEqual(R, [1, 2, 3])
})
// ----------------------------------------------------------------
// Elements
// ----------------------------------------------------------------
it('Should should retain array and only initialize undefined elements', () => {
const T = Type.Array(Type.Literal('hello', { default: 'hello' }))
const R = Value.Default(T, [1, undefined, 3])
Assert.IsEqual(R, [1, 'hello', 3])
})
// ----------------------------------------------------------------
// https://github.com/ubiquity-os-marketplace/command-start-stop/pull/86
// ----------------------------------------------------------------
it('Should retain arrays 1', () => {
const U = Type.Union([Type.Literal('A'), Type.Literal('B'), Type.Literal('C'), Type.Literal('D')], { default: 'A' })
const T = Type.Array(U, { default: ['A', 'B', 'C', 'D'], uniqueItems: true })
Assert.IsEqual(Value.Default(T, undefined), ['A', 'B', 'C', 'D'])
Assert.IsEqual(Value.Default(T, []), [])
Assert.IsEqual(Value.Default(T, ['A']), ['A'])
// initialize undefined element
Assert.IsEqual(Value.Default(T, [undefined, 'B', 'C', 'D']), ['A', 'B', 'C', 'D'])
})
it('Should retain arrays 2', () => {
const U = Type.Union([Type.Literal('A'), Type.Literal('B'), Type.Literal('C'), Type.Literal('D')], { default: 'A' })
// undefined first element initialized by union default
const T = Type.Array(U, { default: [undefined, 'B', 'C', 'D'], uniqueItems: true })
Assert.IsEqual(Value.Default(T, undefined), ['A', 'B', 'C', 'D'])
})
})
Loading