Skip to content

Commit

Permalink
fix: revert created/updated of BaseDBEntity to still be optional
Browse files Browse the repository at this point in the history
  • Loading branch information
kirillgroshkov committed Jan 21, 2024
1 parent fcb1fac commit 1cdc9ba
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 21 deletions.
4 changes: 0 additions & 4 deletions src/json-schema/__snapshots__/jsonSchemaBuilder.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@ exports[`addressBMJsonSchema 1`] = `
"address1",
"city",
"countryCode",
"created",
"id",
"updated",
"zip",
],
"type": "object",
Expand Down Expand Up @@ -97,9 +95,7 @@ exports[`addressDBMJsonSchema 1`] = `
"address1",
"city",
"countryCode",
"created",
"id",
"updated",
"zip",
],
"type": "object",
Expand Down
4 changes: 2 additions & 2 deletions src/json-schema/jsonSchemaBuilder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ const addressDBMJsonSchema2 = baseDBEntityJsonSchema.extend(addressJsonSchema)
const addressBMJsonSchema3 = addressJsonSchema.extend(
jsonSchema.object<BaseDBEntity>({
id: jsonSchema.string(),
created: jsonSchema.unixTimestamp2000(),
updated: jsonSchema.unixTimestamp2000(),
created: jsonSchema.unixTimestamp2000().optional(),
updated: jsonSchema.unixTimestamp2000().optional(),
}),
)

Expand Down
2 changes: 1 addition & 1 deletion src/json-schema/jsonSchemaBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ export class JsonSchemaObjectBuilder<T extends AnyObject> extends JsonSchemaAnyB
updated: { type: 'number', format: 'unixTimestamp2000' },
})

return this.addRequired(['id', 'created', 'updated']) as any
return this.addRequired(['id']) as any
}

extend<T2 extends AnyObject>(s2: JsonSchemaObjectBuilder<T2>): JsonSchemaObjectBuilder<T & T2> {
Expand Down
4 changes: 2 additions & 2 deletions src/json-schema/jsonSchemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { jsonSchema } from './jsonSchemaBuilder'

export const baseDBEntityJsonSchema = jsonSchema.object<BaseDBEntity>({
id: jsonSchema.string(),
created: jsonSchema.unixTimestamp2000(),
updated: jsonSchema.unixTimestamp2000(),
created: jsonSchema.unixTimestamp2000().optional(),
updated: jsonSchema.unixTimestamp2000().optional(),
})

// export const savedDBEntityJsonSchema = jsonSchema.object<SavedDBEntity>({
Expand Down
13 changes: 11 additions & 2 deletions src/types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type {
UnsavedId,
AnyObject,
MonthId,
Saved,
} from './types'
import {
_noop,
Expand Down Expand Up @@ -66,6 +67,14 @@ test('saved/unsaved', () => {
delete itemDBM.a

expectTypeOf(itemDBM).toEqualTypeOf<{
id: string
created?: number
updated?: number
a?: number
}>()

const savedItemDBM = itemDBM as Saved<ItemDBM>
expectTypeOf(savedItemDBM).toEqualTypeOf<{
id: string
created: number
updated: number
Expand Down Expand Up @@ -106,8 +115,8 @@ test('saved/unsaved', () => {

expectTypeOf(unsavedItemId).toEqualTypeOf<{
id?: string
created: number
updated: number
created?: number
updated?: number
a?: number
}>()
})
Expand Down
15 changes: 11 additions & 4 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,25 @@ export type BaseDBEntity = {
/**
* unixTimestamp of when the entity was first created (in the DB).
*/
created: UnixTimestampNumber
created?: UnixTimestampNumber

/**
* unixTimestamp of when the entity was last updated (in the DB).
*/
updated: UnixTimestampNumber
updated?: UnixTimestampNumber
}

export type Saved<T> = T & BaseDBEntity
export type Saved<T> = T & {
created: UnixTimestampNumber
updated: UnixTimestampNumber
}

export type Unsaved<T> = T extends AnyObject
? Omit<T, 'id' | 'created' | 'updated'> & Partial<BaseDBEntity>
? Omit<T, 'id' | 'created' | 'updated'> & {
id?: string
created?: UnixTimestampNumber
updated?: UnixTimestampNumber
}
: T

export type UnsavedId<T> = Omit<T, 'id'> & {
Expand Down
4 changes: 2 additions & 2 deletions src/zod/zod.shared.schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ export const zSlug = z
export const zBaseDBEntity = z
.object({
id: z.string(),
created: zUnixTimestamp2000,
updated: zUnixTimestamp2000,
created: zUnixTimestamp2000.optional(),
updated: zUnixTimestamp2000.optional(),
})
.describe('BaseDBEntity')

Expand Down
5 changes: 1 addition & 4 deletions src/zod/zod.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ test('basic', () => {
Input:
{}
3 issues:
id: Required
created: Required
updated: Required"
id: Required"
`)

expect(zSafeValidate(' a' as any, zBaseDBEntity).error!.message).toMatchInlineSnapshot(`
Expand Down

0 comments on commit 1cdc9ba

Please sign in to comment.