Skip to content

Commit

Permalink
chore(action-import): validate association & select field value (#4643)
Browse files Browse the repository at this point in the history
* chore: validate association value in import

* chore: validate value in select field

* fix: test
  • Loading branch information
chareice committed Jun 14, 2024
1 parent 8260cfc commit e56e52a
Show file tree
Hide file tree
Showing 6 changed files with 362 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,22 @@ export class MultipleSelectInterface extends BaseInterface {
const enumConfig = this.options.uiSchema?.enum || [];
return items.map((item) => {
const option = enumConfig.find((option) => option.label === item);
return option ? option.value : item;
if (option) {
return option.value;
}

const valueOption = enumConfig.find((option) => option.value === item);
if (valueOption) {
return valueOption.value;
}

throw new Error(`"${item}" is not a valid option in ${ctx.field.name} field.`);
});
}

toString(value: any, ctx?: any) {
const enumConfig = this.options.uiSchema?.enum || [];

return lodash
.castArray(value)
.map((value) => {
Expand Down
15 changes: 14 additions & 1 deletion packages/core/database/src/interfaces/select-interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,22 @@ import { BaseInterface } from './base-interface';

export class SelectInterface extends BaseInterface {
async toValue(str: string, ctx?: any): Promise<any> {
if (!str) {
return null;
}

const enumConfig = this.options.uiSchema?.enum || [];
const option = enumConfig.find((item) => item.label === str);
return option?.value || str;
if (option) {
return option.value;
}

const valueOption = enumConfig.find((item) => item.value === str);
if (valueOption) {
return valueOption.value;
}

throw new Error(`"${str}" is not a valid option in ${ctx.field.name} field.`);
}

toString(value: any, ctx?: any) {
Expand Down
7 changes: 7 additions & 0 deletions packages/core/database/src/interfaces/to-many-interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ export class ToManyInterface extends BaseInterface {
transaction,
});

// check if all items are found
items.forEach((item) => {
if (!targetInstances.find((targetInstance) => targetInstance[filterKey] === item)) {
throw new Error(`"${item}" not found in ${targetCollection.model.name} ${filterKey}`);
}
});

const primaryKeyAttribute = targetCollection.model.primaryKeyAttribute;

return targetInstances.map((targetInstance) => targetInstance[primaryKeyAttribute]);
Expand Down
13 changes: 8 additions & 5 deletions packages/core/database/src/interfaces/to-one-interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ export class ToOneInterface extends BaseInterface {
}

async toValue(str: string, ctx?: any) {
if (!str) {
return null;
}

const { filterKey, targetCollection, transaction } = ctx;

const targetInstance = await targetCollection.repository.findOne({
Expand All @@ -24,12 +28,11 @@ export class ToOneInterface extends BaseInterface {
transaction,
});

const primaryKeyAttribute = targetCollection.model.primaryKeyAttribute;

if (targetInstance) {
return targetInstance[primaryKeyAttribute];
if (!targetInstance) {
throw new Error(`"${str}" not found in ${targetCollection.model.name} ${filterKey}`);
}
const primaryKeyAttribute = targetCollection.model.primaryKeyAttribute;

return null;
return targetInstance[primaryKeyAttribute];
}
}
Loading

0 comments on commit e56e52a

Please sign in to comment.