fix(db-mongodb): localized dates being returned as date objects instead of strings (#12354)

Fixes https://github.com/payloadcms/payload/issues/12334

We weren't passing locale through to the Date transformer function so
localized dates were being read as objects instead of strings.
This commit is contained in:
Paul
2025-05-10 17:15:15 -07:00
committed by GitHub
parent 3701de5056
commit c43891b2ba
6 changed files with 90 additions and 0 deletions

View File

@@ -0,0 +1,21 @@
import type { CollectionConfig } from 'payload'
import { localizedDateFieldsSlug } from '../../shared.js'
export const LocalizedDateFields: CollectionConfig = {
slug: localizedDateFieldsSlug,
versions: {
drafts: true,
},
fields: [
{
type: 'date',
name: 'localizedDate',
localized: true,
},
{
type: 'date',
name: 'date',
},
],
}

View File

@@ -11,6 +11,7 @@ import { devUser } from '../credentials.js'
import { ArrayCollection } from './collections/Array/index.js'
import { BlocksCollection } from './collections/Blocks/index.js'
import { Group } from './collections/Group/index.js'
import { LocalizedDateFields } from './collections/LocalizedDateFields/index.js'
import { LocalizedDrafts } from './collections/LocalizedDrafts/index.js'
import { LocalizedWithinLocalized } from './collections/LocalizedWithinLocalized/index.js'
import { NestedArray } from './collections/NestedArray/index.js'
@@ -25,6 +26,7 @@ import {
defaultLocale,
englishTitle,
hungarianLocale,
localizedDateFieldsSlug,
localizedPostsSlug,
localizedSortSlug,
portugueseLocale,
@@ -64,6 +66,7 @@ export default buildConfigWithDefaults({
NestedArray,
NestedFields,
LocalizedDrafts,
LocalizedDateFields,
{
admin: {
listSearchableFields: 'name',
@@ -478,6 +481,14 @@ export default buildConfigWithDefaults({
},
})
await payload.create({
collection: localizedDateFieldsSlug,
data: {
localizedDate: new Date().toISOString(),
date: new Date().toISOString(),
},
})
console.log('SEED 1')
await payload.create({

View File

@@ -27,6 +27,7 @@ import {
defaultLocale as englishLocale,
englishTitle,
hungarianLocale,
localizedDateFieldsSlug,
localizedPostsSlug,
localizedSortSlug,
portugueseLocale,
@@ -431,6 +432,32 @@ describe('Localization', () => {
})
})
describe('Localized date', () => {
it('can create a localized date', async () => {
const document = await payload.create({
collection: localizedDateFieldsSlug,
data: {
localizedDate: new Date().toISOString(),
date: new Date().toISOString(),
},
})
expect(document.localizedDate).toBeTruthy()
})
it('data is typed as string', async () => {
const document = await payload.create({
collection: localizedDateFieldsSlug,
data: {
localizedDate: new Date().toISOString(),
date: new Date().toISOString(),
},
})
expect(typeof document.localizedDate).toBe('string')
expect(typeof document.date).toBe('string')
})
})
describe('Localized Sort Count', () => {
const expectedTotalDocs = 5
const posts: LocalizedSort[] = []

View File

@@ -72,6 +72,7 @@ export interface Config {
'nested-arrays': NestedArray;
'nested-field-tables': NestedFieldTable;
'localized-drafts': LocalizedDraft;
'localized-date-fields': LocalizedDateField;
users: User;
'localized-posts': LocalizedPost;
'no-localized-fields': NoLocalizedField;
@@ -97,6 +98,7 @@ export interface Config {
'nested-arrays': NestedArraysSelect<false> | NestedArraysSelect<true>;
'nested-field-tables': NestedFieldTablesSelect<false> | NestedFieldTablesSelect<true>;
'localized-drafts': LocalizedDraftsSelect<false> | LocalizedDraftsSelect<true>;
'localized-date-fields': LocalizedDateFieldsSelect<false> | LocalizedDateFieldsSelect<true>;
users: UsersSelect<false> | UsersSelect<true>;
'localized-posts': LocalizedPostsSelect<false> | LocalizedPostsSelect<true>;
'no-localized-fields': NoLocalizedFieldsSelect<false> | NoLocalizedFieldsSelect<true>;
@@ -330,6 +332,18 @@ export interface LocalizedDraft {
createdAt: string;
_status?: ('draft' | 'published') | null;
}
/**
* This interface was referenced by `Config`'s JSON-Schema
* via the `definition` "localized-date-fields".
*/
export interface LocalizedDateField {
id: string;
localizedDate?: string | null;
date?: string | null;
updatedAt: string;
createdAt: string;
_status?: ('draft' | 'published') | null;
}
/**
* This interface was referenced by `Config`'s JSON-Schema
* via the `definition` "users".
@@ -713,6 +727,10 @@ export interface PayloadLockedDocument {
relationTo: 'localized-drafts';
value: string | LocalizedDraft;
} | null)
| ({
relationTo: 'localized-date-fields';
value: string | LocalizedDateField;
} | null)
| ({
relationTo: 'users';
value: string | User;
@@ -952,6 +970,17 @@ export interface LocalizedDraftsSelect<T extends boolean = true> {
createdAt?: T;
_status?: T;
}
/**
* This interface was referenced by `Config`'s JSON-Schema
* via the `definition` "localized-date-fields_select".
*/
export interface LocalizedDateFieldsSelect<T extends boolean = true> {
localizedDate?: T;
date?: T;
updatedAt?: T;
createdAt?: T;
_status?: T;
}
/**
* This interface was referenced by `Config`'s JSON-Schema
* via the `definition` "users_select".

View File

@@ -12,6 +12,7 @@ export const hungarianLocale = 'hu'
// Slugs
export const localizedPostsSlug = 'localized-posts'
export const localizedDateFieldsSlug = 'localized-date-fields'
export const withLocalizedRelSlug = 'with-localized-relationship'
export const relationshipLocalizedSlug = 'relationship-localized'
export const withRequiredLocalizedFields = 'localized-required'