fix: allows empty objects to be retained in db
This commit is contained in:
@@ -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,
|
||||
},
|
||||
);
|
||||
|
||||
@@ -70,6 +70,7 @@ export default function initCollectionsLocal(ctx: Payload): void {
|
||||
draftsEnabled: true,
|
||||
options: {
|
||||
timestamps: false,
|
||||
minimize: false,
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
|
||||
|
||||
@@ -27,6 +27,7 @@ export default function initGlobalsLocal(ctx: Payload): void {
|
||||
draftsEnabled: true,
|
||||
options: {
|
||||
timestamps: false,
|
||||
minimize: false,
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
@@ -40,7 +40,6 @@ export type BuildSchemaOptions = {
|
||||
allowIDField?: boolean
|
||||
disableUnique?: boolean
|
||||
draftsEnabled?: boolean
|
||||
global?: boolean
|
||||
indexSortableFields?: boolean
|
||||
}
|
||||
|
||||
|
||||
@@ -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', () => {
|
||||
|
||||
@@ -24,6 +24,10 @@ export default buildConfig({
|
||||
slug,
|
||||
access,
|
||||
fields: [
|
||||
{
|
||||
name: 'json',
|
||||
type: 'json',
|
||||
},
|
||||
{
|
||||
name: 'title',
|
||||
type: 'text',
|
||||
|
||||
@@ -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',
|
||||
|
||||
Reference in New Issue
Block a user