Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extends Optional Property Check #650

Merged
merged 2 commits into from
Oct 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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)
})
})
Loading