fix: allows empty objects to be retained in db

This commit is contained in:
Jarrod Flesch
2023-02-28 10:40:21 -05:00
parent 5ac436e184
commit 0247e2d106
8 changed files with 47 additions and 4 deletions

View File

@@ -11,7 +11,11 @@ const buildCollectionSchema = (collection: SanitizedCollectionConfig, config: Sa
collection.fields,
{
draftsEnabled: Boolean(typeof collection?.versions === 'object' && collection.versions.drafts),
options: { timestamps: collection.timestamps !== false, ...schemaOptions },
options: {
timestamps: collection.timestamps !== false,
minimize: false,
...schemaOptions,
},
indexSortableFields: config.indexSortableFields,
},
);

View File

@@ -70,6 +70,7 @@ export default function initCollectionsLocal(ctx: Payload): void {
draftsEnabled: true,
options: {
timestamps: false,
minimize: false,
},
},
);

View File

@@ -6,14 +6,22 @@ import { GlobalModel } from './config/types';
const buildModel = (config: SanitizedConfig): GlobalModel | null => {
if (config.globals && config.globals.length > 0) {
const globalsSchema = new mongoose.Schema({}, { discriminatorKey: 'globalType', timestamps: true });
const globalsSchema = new mongoose.Schema({}, { discriminatorKey: 'globalType', timestamps: true, minimize: false });
globalsSchema.plugin(buildQueryPlugin);
const Globals = mongoose.model('globals', globalsSchema) as unknown as GlobalModel;
Object.values(config.globals).forEach((globalConfig) => {
const globalSchema = buildSchema(config, globalConfig.fields, { global: true });
const globalSchema = buildSchema(
config,
globalConfig.fields,
{
options: {
minimize: false,
},
},
);
Globals.discriminator(globalConfig.slug, globalSchema);
});

View File

@@ -27,6 +27,7 @@ export default function initGlobalsLocal(ctx: Payload): void {
draftsEnabled: true,
options: {
timestamps: false,
minimize: false,
},
},
);

View File

@@ -40,7 +40,6 @@ export type BuildSchemaOptions = {
allowIDField?: boolean
disableUnique?: boolean
draftsEnabled?: boolean
global?: boolean
indexSortableFields?: boolean
}

View File

@@ -524,6 +524,19 @@ describe('Fields', () => {
},
})).rejects.toThrow('The following field is invalid: json');
});
it('should save empty json objects', async () => {
const createdJSON = await payload.create({
collection: 'json-fields',
data: {
json: {
state: {},
},
},
});
expect(createdJSON.json.state).toBeDefined();
});
});
describe('richText', () => {

View File

@@ -24,6 +24,10 @@ export default buildConfig({
slug,
access,
fields: [
{
name: 'json',
type: 'json',
},
{
name: 'title',
type: 'text',

View File

@@ -60,6 +60,19 @@ describe('globals', () => {
});
describe('local', () => {
it('should save empty json objects', async () => {
const createdJSON = await payload.updateGlobal({
slug,
data: {
json: {
state: {},
},
},
});
expect(createdJSON.json.state).toBeDefined();
});
it('should create', async () => {
const data = {
title: 'title',