Skip to content

Commit

Permalink
Extends Optional Property Check
Browse files Browse the repository at this point in the history
  • Loading branch information
sinclairzx81 committed Oct 29, 2023
1 parent a9cfd7e commit 03cf2fe
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 34 deletions.
78 changes: 44 additions & 34 deletions src/typebox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1144,6 +1144,9 @@ export namespace TypeGuard {
function IsOptionalSchema(value: unknown): value is boolean | undefined {
return ValueGuard.IsUndefined(value) || TSchema(value)
}
// ----------------------------------------------------------------
// Types
// ----------------------------------------------------------------
/** Returns true if the given value is TAny */
export function TAny(schema: unknown): schema is TAny {
// prettier-ignore
Expand Down Expand Up @@ -1533,40 +1536,42 @@ export namespace TypeGuard {
}
/** Returns true if the given value is TSchema */
export function TSchema(schema: unknown): schema is TSchema {
// prettier-ignore
return (
ValueGuard.IsObject(schema) &&
(TAny(schema) ||
TArray(schema) ||
TBoolean(schema) ||
TBigInt(schema) ||
TAsyncIterator(schema) ||
TConstructor(schema) ||
TDate(schema) ||
TFunction(schema) ||
TInteger(schema) ||
TIntersect(schema) ||
TIterator(schema) ||
TLiteral(schema) ||
TNever(schema) ||
TNot(schema) ||
TNull(schema) ||
TNumber(schema) ||
TObject(schema) ||
TPromise(schema) ||
TRecord(schema) ||
TRef(schema) ||
TString(schema) ||
TSymbol(schema) ||
TTemplateLiteral(schema) ||
TThis(schema) ||
TTuple(schema) ||
TUndefined(schema) ||
TUnion(schema) ||
TUint8Array(schema) ||
TUnknown(schema) ||
TUnsafe(schema) ||
TVoid(schema) ||
(TKind(schema) && TypeRegistry.Has(schema[Kind] as any)))
ValueGuard.IsObject(schema)
) && (
TAny(schema) ||
TArray(schema) ||
TBoolean(schema) ||
TBigInt(schema) ||
TAsyncIterator(schema) ||
TConstructor(schema) ||
TDate(schema) ||
TFunction(schema) ||
TInteger(schema) ||
TIntersect(schema) ||
TIterator(schema) ||
TLiteral(schema) ||
TNever(schema) ||
TNot(schema) ||
TNull(schema) ||
TNumber(schema) ||
TObject(schema) ||
TPromise(schema) ||
TRecord(schema) ||
TRef(schema) ||
TString(schema) ||
TSymbol(schema) ||
TTemplateLiteral(schema) ||
TThis(schema) ||
TTuple(schema) ||
TUndefined(schema) ||
TUnion(schema) ||
TUint8Array(schema) ||
TUnknown(schema) ||
TUnsafe(schema) ||
TVoid(schema) ||
(TKind(schema) && TypeRegistry.Has(schema[Kind] as any))
)
}
}
Expand Down Expand Up @@ -1986,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
39 changes: 39 additions & 0 deletions test/runtime/type/extends/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,43 @@ describe('type/extends/Object', () => {
const R = TypeExtends.Extends(Type.Object({ a: Type.Number() }), Type.Date())
Assert.IsEqual(R, TypeExtendsResult.False)
})
// ----------------------------------------------------------------
// Issues
// ----------------------------------------------------------------
it('Should extend optional 1', () => {
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 2', () => {
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 3', () => {
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 03cf2fe

Please sign in to comment.