Revision 0.33.15 (#1025)

* Add Resolver for Default Date

* Version

* ChangeLog
This commit is contained in:
Haydn Paterson
2024-10-07 12:08:27 +09:00
committed by GitHub
parent 99f4d9c1a1
commit f9134bbadf
7 changed files with 42 additions and 6 deletions

View File

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

4
package-lock.json generated
View File

@@ -1,12 +1,12 @@
{
"name": "@sinclair/typebox",
"version": "0.33.14",
"version": "0.33.15",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "@sinclair/typebox",
"version": "0.33.14",
"version": "0.33.15",
"license": "MIT",
"devDependencies": {
"@arethetypeswrong/cli": "^0.13.2",

View File

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

View File

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

View File

@@ -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
// ------------------------------------------------------------------
@@ -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) => {
@@ -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':

View File

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

View File

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