chore: adjusts block and array schemas to store undefined instead of empty arrays by default
This commit is contained in:
@@ -50,7 +50,10 @@ export const promise = async ({
|
|||||||
skipValidation,
|
skipValidation,
|
||||||
}: Args): Promise<void> => {
|
}: Args): Promise<void> => {
|
||||||
const passesCondition = (field.admin?.condition) ? field.admin.condition(data, siblingData) : true;
|
const passesCondition = (field.admin?.condition) ? field.admin.condition(data, siblingData) : true;
|
||||||
const skipValidationFromHere = skipValidation || !passesCondition;
|
let skipValidationFromHere = skipValidation || !passesCondition;
|
||||||
|
|
||||||
|
const defaultLocale = req.payload.config?.localization ? req.payload.config.localization?.defaultLocale : 'en';
|
||||||
|
const operationLocale = req.locale || defaultLocale;
|
||||||
|
|
||||||
if (fieldAffectsData(field)) {
|
if (fieldAffectsData(field)) {
|
||||||
if (typeof siblingData[field.name] === 'undefined') {
|
if (typeof siblingData[field.name] === 'undefined') {
|
||||||
@@ -73,6 +76,17 @@ export const promise = async ({
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (siblingData[field.name] === null) {
|
||||||
|
if (field.localized && ['array', 'blocks'].includes(field.type)) {
|
||||||
|
if (operationLocale !== defaultLocale) {
|
||||||
|
// localized fields set to null and not the default locale, should be set to undefined
|
||||||
|
// this way fallback locale can be used
|
||||||
|
siblingData[field.name] = undefined;
|
||||||
|
skipValidationFromHere = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Execute hooks
|
// Execute hooks
|
||||||
if (field.hooks?.beforeChange) {
|
if (field.hooks?.beforeChange) {
|
||||||
await field.hooks.beforeChange.reduce(async (priorHook, currentHook) => {
|
await field.hooks.beforeChange.reduce(async (priorHook, currentHook) => {
|
||||||
@@ -95,13 +109,11 @@ export const promise = async ({
|
|||||||
|
|
||||||
// Validate
|
// Validate
|
||||||
if (!skipValidationFromHere && field.validate) {
|
if (!skipValidationFromHere && field.validate) {
|
||||||
let valueToValidate;
|
let valueToValidate = siblingData[field.name];
|
||||||
|
|
||||||
if (['array', 'blocks'].includes(field.type)) {
|
if (['array', 'blocks'].includes(field.type)) {
|
||||||
const rows = siblingData[field.name];
|
const rows = siblingData[field.name];
|
||||||
valueToValidate = Array.isArray(rows) ? rows.length : 0;
|
valueToValidate = Array.isArray(rows) ? rows.length : 0;
|
||||||
} else {
|
|
||||||
valueToValidate = siblingData[field.name];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const validationResult = await field.validate(valueToValidate, {
|
const validationResult = await field.validate(valueToValidate, {
|
||||||
@@ -127,21 +139,20 @@ export const promise = async ({
|
|||||||
if (field.localized) {
|
if (field.localized) {
|
||||||
mergeLocaleActions.push(() => {
|
mergeLocaleActions.push(() => {
|
||||||
if (req.payload.config.localization) {
|
if (req.payload.config.localization) {
|
||||||
const localeData = req.payload.config.localization.locales.reduce((locales, localeID) => {
|
const localeData = req.payload.config.localization.locales.reduce((localizedValues, locale) => {
|
||||||
let valueToSet = siblingData[field.name];
|
const fieldValue = locale === req.locale
|
||||||
|
? siblingData[field.name]
|
||||||
|
: siblingDocWithLocales?.[field.name]?.[locale];
|
||||||
|
|
||||||
if (localeID !== req.locale) {
|
// update locale value if it's not undefined
|
||||||
valueToSet = siblingDocWithLocales?.[field.name]?.[localeID];
|
if (typeof fieldValue !== 'undefined') {
|
||||||
}
|
|
||||||
|
|
||||||
if (typeof valueToSet !== 'undefined') {
|
|
||||||
return {
|
return {
|
||||||
...locales,
|
...localizedValues,
|
||||||
[localeID]: valueToSet,
|
[locale]: fieldValue,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
return locales;
|
return localizedValues;
|
||||||
}, {});
|
}, {});
|
||||||
|
|
||||||
// If there are locales with data, set the data
|
// If there are locales with data, set the data
|
||||||
@@ -221,7 +232,6 @@ export const promise = async ({
|
|||||||
await Promise.all(promises);
|
await Promise.all(promises);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -256,7 +266,6 @@ export const promise = async ({
|
|||||||
await Promise.all(promises);
|
await Promise.all(promises);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -330,6 +330,7 @@ const fieldToSchemaMap: Record<string, FieldSchemaGenerator> = {
|
|||||||
array: (field: ArrayField, schema: Schema, config: SanitizedConfig, buildSchemaOptions: BuildSchemaOptions) => {
|
array: (field: ArrayField, schema: Schema, config: SanitizedConfig, buildSchemaOptions: BuildSchemaOptions) => {
|
||||||
const baseSchema = {
|
const baseSchema = {
|
||||||
...formatBaseSchema(field, buildSchemaOptions),
|
...formatBaseSchema(field, buildSchemaOptions),
|
||||||
|
default: undefined,
|
||||||
type: [buildSchema(
|
type: [buildSchema(
|
||||||
config,
|
config,
|
||||||
field.fields,
|
field.fields,
|
||||||
@@ -392,7 +393,10 @@ const fieldToSchemaMap: Record<string, FieldSchemaGenerator> = {
|
|||||||
});
|
});
|
||||||
},
|
},
|
||||||
blocks: (field: BlockField, schema: Schema, config: SanitizedConfig, buildSchemaOptions: BuildSchemaOptions): void => {
|
blocks: (field: BlockField, schema: Schema, config: SanitizedConfig, buildSchemaOptions: BuildSchemaOptions): void => {
|
||||||
const fieldSchema = [new Schema({}, { _id: false, discriminatorKey: 'blockType' })];
|
const fieldSchema = {
|
||||||
|
default: undefined,
|
||||||
|
type: [new Schema({}, { _id: false, discriminatorKey: 'blockType' })],
|
||||||
|
};
|
||||||
|
|
||||||
schema.add({
|
schema.add({
|
||||||
[field.name]: localizeSchema(field, fieldSchema, config.localization),
|
[field.name]: localizeSchema(field, fieldSchema, config.localization),
|
||||||
|
|||||||
@@ -57,6 +57,7 @@ export default buildConfig({
|
|||||||
localization: {
|
localization: {
|
||||||
defaultLocale: 'en',
|
defaultLocale: 'en',
|
||||||
locales: ['en', 'es'],
|
locales: ['en', 'es'],
|
||||||
|
fallback: true,
|
||||||
},
|
},
|
||||||
onInit: async (payload) => {
|
onInit: async (payload) => {
|
||||||
await payload.create({
|
await payload.create({
|
||||||
|
|||||||
@@ -276,13 +276,13 @@ describe('Fields', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return empty array for arrays when no data present', async () => {
|
it('should return undefined arrays when no data present', async () => {
|
||||||
const document = await payload.create<ArrayField>({
|
const document = await payload.create<ArrayField>({
|
||||||
collection: arrayFieldsSlug,
|
collection: arrayFieldsSlug,
|
||||||
data: arrayDoc,
|
data: arrayDoc,
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(document.potentiallyEmptyArray).toEqual([]);
|
expect(document.potentiallyEmptyArray).toBeUndefined();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should create with ids and nested ids', async () => {
|
it('should create with ids and nested ids', async () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user