Skip to content

Commit

Permalink
Revision 0.32.30 (#868)
Browse files Browse the repository at this point in the history
* Support Encode Decode for Null Prototype

* Version
  • Loading branch information
sinclairzx81 committed May 11, 2024
1 parent 7a42aee commit 24c8639
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 5 deletions.
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.32.29",
"version": "0.32.30",
"description": "Json Schema Type Builder with Static Type Resolution for TypeScript",
"keywords": [
"typescript",
Expand Down
2 changes: 1 addition & 1 deletion src/value/guard/guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export function IsIterator(value: unknown): value is IterableIterator<any> {
// --------------------------------------------------------------------------
/** Returns true if this value is not an instance of a class */
export function IsStandardObject(value: unknown): value is ObjectType {
return IsObject(value) && !IsArray(value) && IsFunction(value.constructor) && value.constructor.name === 'Object'
return IsObject(value) && (Object.getPrototypeOf(value) === Object.prototype || Object.getPrototypeOf(value) === null)
}
/** Returns true if this value is an instance of a class */
export function IsInstanceObject(value: unknown): value is ObjectType {
Expand Down
4 changes: 4 additions & 0 deletions test/runtime/value/guard/guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,10 @@ describe('value/guard/ValueGuard', () => {
const R = ValueGuard.IsStandardObject(new (class {})())
Assert.IsFalse(R)
})
it('Should guard StandardObject 5', () => {
const R = ValueGuard.IsStandardObject(Object.create(null))
Assert.IsTrue(R)
})
// -----------------------------------------------------
// IsInstanceObject
// -----------------------------------------------------
Expand Down
17 changes: 16 additions & 1 deletion test/runtime/value/transform/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Assert } from '../../assert'
import { Value } from '@sinclair/typebox/value'
import { Type } from '@sinclair/typebox'

// prettier-ignore
describe('value/transform/Object', () => {
// --------------------------------------------------------
// Identity
Expand Down Expand Up @@ -154,7 +155,6 @@ describe('value/transform/Object', () => {
// ----------------------------------------------------------------
// https://github.com/sinclairzx81/typebox/issues/859
// ----------------------------------------------------------------
// prettier-ignore
it('Should decode for nested transform with renamed property', () => {
class User { constructor(public name: string, public createdAt: Date) {} }
const TDate = Type.Transform(Type.Number())
Expand All @@ -173,4 +173,19 @@ describe('value/transform/Object', () => {
Assert.IsEqual(E.name, 'name')
Assert.IsEqual(E.created_at, 0)
})
// ----------------------------------------------------------------
// https://github.com/sinclairzx81/typebox/issues/865
// ----------------------------------------------------------------
it('Should decode for null prototype', () => {
const N = Type.Transform(Type.Number())
.Decode(value => value.toString())
.Encode(value => parseInt(value))
const T = Type.Object({ x: N })
const A = Object.create(null); A.x = 1
const B = Object.create(null); B.x = '1'
const D = Value.Decode(T, A)
const E = Value.Encode(T, B)
Assert.IsEqual(D, { x: '1' })
Assert.IsEqual(E, { x: 1 })
})
})

0 comments on commit 24c8639

Please sign in to comment.