Skip to content

Commit

Permalink
Revision 0.33.15 (#1025)
Browse files Browse the repository at this point in the history
* Add Resolver for Default Date

* Version

* ChangeLog
  • Loading branch information
sinclairzx81 authored Oct 7, 2024
1 parent 99f4d9c commit f9134bb
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 6 deletions.
2 changes: 2 additions & 0 deletions changelog/0.33.0.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
### 0.33.0
- [Revision 0.33.15](https://github.com/sinclairzx81/typebox/pull/1025)
- [1024](https://github.com/sinclairzx81/typebox/issues/1024) Fix to correctly resolve default Dates
- [Revision 0.33.14](https://github.com/sinclairzx81/typebox/pull/1019)
- [1019](https://github.com/sinclairzx81/typebox/pull/1019) Converting Large Numbers to BigInt
- [Revision 0.33.13](https://github.com/sinclairzx81/typebox/pull/1011)
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.33.14",
"version": "0.33.15",
"description": "Json Schema Type Builder with Static Type Resolution for TypeScript",
"keywords": [
"typescript",
Expand Down
7 changes: 7 additions & 0 deletions src/type/guard/value.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ THE SOFTWARE.
---------------------------------------------------------------------------*/

// --------------------------------------------------------------------------
// PropertyKey
// --------------------------------------------------------------------------
/** Returns true if this value has this property key */
export function HasPropertyKey<K extends PropertyKey>(value: Record<any, unknown>, key: K): value is Record<PropertyKey, unknown> & { [_ in K]: unknown } {
return key in value
}
// --------------------------------------------------------------------------
// Object Instances
// --------------------------------------------------------------------------
Expand Down
8 changes: 7 additions & 1 deletion src/value/default/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ import type { TUnion } from '../../type/union/index'
// ------------------------------------------------------------------
// ValueGuard
// ------------------------------------------------------------------
import { IsFunction, IsObject, IsArray, IsUndefined, HasPropertyKey } from '../guard/index'
import { IsArray, IsDate, IsFunction, IsObject, IsUndefined, HasPropertyKey } from '../guard/index'
// ------------------------------------------------------------------
// TypeGuard
// ------------------------------------------------------------------
Expand Down Expand Up @@ -74,6 +74,10 @@ function FromArray(schema: TArray, references: TSchema[], value: unknown): any {
}
return defaulted
}
function FromDate(schema: TArray, references: TSchema[], value: unknown): any {
// special case intercept for dates
return IsDate(value) ? value : ValueOrDefault(schema, value)
}
function FromIntersect(schema: TIntersect, references: TSchema[], value: unknown): any {
const defaulted = ValueOrDefault(schema, value)
return schema.allOf.reduce((acc, schema) => {
Expand Down Expand Up @@ -155,6 +159,8 @@ function Visit(schema: TSchema, references: TSchema[], value: unknown): any {
switch (schema_[Kind]) {
case 'Array':
return FromArray(schema_, references_, value)
case 'Date':
return FromDate(schema_, references_, value)
case 'Intersect':
return FromIntersect(schema_, references_, value)
case 'Object':
Expand Down
7 changes: 5 additions & 2 deletions src/value/guard/guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,15 @@ export function IsBigUint64Array(value: unknown): value is BigUint64Array {
return value instanceof globalThis.BigUint64Array
}
// --------------------------------------------------------------------------
// Standard
// PropertyKey
// --------------------------------------------------------------------------
/** Returns true if this value has this property key */
export function HasPropertyKey<K extends PropertyKey>(value: Record<any, unknown>, key: K): value is ObjectType & Record<K, unknown> {
export function HasPropertyKey<K extends PropertyKey>(value: Record<any, unknown>, key: K): value is Record<PropertyKey, unknown> & { [_ in K]: unknown } {
return key in value
}
// --------------------------------------------------------------------------
// Standard
// --------------------------------------------------------------------------
/** Returns true of this value is an object type */
export function IsObject(value: unknown): value is ObjectType {
return value !== null && typeof value === 'object'
Expand Down
18 changes: 18 additions & 0 deletions test/runtime/value/default/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,22 @@ describe('value/default/Date', () => {
const R = Value.Default(T, null)
Assert.IsEqual(R, null)
})
// ----------------------------------------------------------------
// https://github.com/sinclairzx81/typebox/issues/1024
// ----------------------------------------------------------------
it('Should use value if Date is valid', () => {
const T = Type.Date({ default: new Date(1) })
const R = Value.Default(T, new Date(2)) as Date
Assert.IsEqual(R.getTime(), 2)
})
it('Should use default if Date is undefined', () => {
const T = Type.Date({ default: new Date(1) })
const R = Value.Default(T, undefined) as Date
Assert.IsEqual(R.getTime(), 1)
})
it('Should use value if Date is invalid', () => {
const T = Type.Date({ default: new Date(1) })
const R = Value.Default(T, null)
Assert.IsEqual(R, null)
})
})

0 comments on commit f9134bb

Please sign in to comment.