Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin' into defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
sinclairzx81 committed Oct 29, 2023
2 parents 4233f59 + ce1ace5 commit c4aff0a
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 4 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.31.20",
"version": "0.31.21",
"description": "JSONSchema Type Builder with Static Type Resolution for TypeScript",
"keywords": [
"typescript",
Expand Down
7 changes: 6 additions & 1 deletion src/typebox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1991,7 +1991,12 @@ export namespace TypeExtends {
!TypeGuard.TObject(right) ? TypeExtendsResult.False :
(() => {
for (const key of Object.getOwnPropertyNames(right.properties)) {
if (!(key in left.properties)) return TypeExtendsResult.False
if (!(key in left.properties) && !TypeGuard.TOptional(right.properties[key])) {
return TypeExtendsResult.False
}
if(TypeGuard.TOptional(right.properties[key])) {
return TypeExtendsResult.True
}
if (Property(left.properties[key], right.properties[key]) === TypeExtendsResult.False) {
return TypeExtendsResult.False
}
Expand Down
51 changes: 51 additions & 0 deletions test/runtime/type/extends/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,55 @@ describe('type/extends/Object', () => {
const R = TypeExtends.Extends(Type.Object({ a: Type.Number() }), Type.Date())
Assert.IsEqual(R, TypeExtendsResult.False)
})
// ----------------------------------------------------------------
// Optional
// ----------------------------------------------------------------
it('Should extend optional 1', () => {
const A = Type.Object({ a: Type.Number() })
const B = Type.Object({ a: Type.Optional(Type.Number()) })
const C = TypeExtends.Extends(A, B)
Assert.IsEqual(C, TypeExtendsResult.True)
})
it('Should extend optional 2', () => {
const A = Type.Object({ a: Type.Number() })
const B = Type.Object({ a: Type.Optional(Type.Number()) })
const C = TypeExtends.Extends(B, A)
Assert.IsEqual(C, TypeExtendsResult.False)
})
it('Should extend optional 3', () => {
const A = Type.Object({
x: Type.Optional(Type.Number()),
y: Type.Number(),
})
const B = Type.Object({
y: Type.Number(),
z: Type.Number(),
})
const R = TypeExtends.Extends(A, B)
Assert.IsEqual(R, TypeExtendsResult.False)
})
it('Should extend optional 4', () => {
const A = Type.Object({
x: Type.Number(),
y: Type.Number(),
})
const B = Type.Object({
y: Type.Number(),
z: Type.Number(),
})
const R = TypeExtends.Extends(A, B)
Assert.IsEqual(R, TypeExtendsResult.False)
})
it('Should extend optional 5', () => {
const A = Type.Object({
x: Type.Number(),
y: Type.Number(),
})
const B = Type.Object({
y: Type.Number(),
z: Type.Optional(Type.Number()),
})
const R = TypeExtends.Extends(A, B)
Assert.IsEqual(R, TypeExtendsResult.True)
})
})

0 comments on commit c4aff0a

Please sign in to comment.