mirror of
https://github.com/shishantbiswas/bknd.git
synced 2026-03-16 04:27:21 +00:00
allow bypassing entity data validation for unknown keys in useEntityForm
This commit is contained in:
@@ -193,29 +193,40 @@ export class Entity<
|
|||||||
}
|
}
|
||||||
|
|
||||||
// @todo: add tests
|
// @todo: add tests
|
||||||
isValidData(data: EntityData, context: TActionContext, explain?: boolean): boolean {
|
isValidData(
|
||||||
|
data: EntityData,
|
||||||
|
context: TActionContext,
|
||||||
|
options?: {
|
||||||
|
explain?: boolean;
|
||||||
|
ignoreUnknown?: boolean;
|
||||||
|
}
|
||||||
|
): boolean {
|
||||||
if (typeof data !== "object") {
|
if (typeof data !== "object") {
|
||||||
if (explain) {
|
if (options?.explain) {
|
||||||
throw new Error(`Entity "${this.name}" data must be an object`);
|
throw new Error(`Entity "${this.name}" data must be an object`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const fields = this.getFillableFields(context, false);
|
const fields = this.getFillableFields(context, false);
|
||||||
|
|
||||||
|
if (options?.ignoreUnknown !== true) {
|
||||||
const field_names = fields.map((f) => f.name);
|
const field_names = fields.map((f) => f.name);
|
||||||
const given_keys = Object.keys(data);
|
const given_keys = Object.keys(data);
|
||||||
|
const unknown_keys = given_keys.filter((key) => !field_names.includes(key));
|
||||||
|
|
||||||
if (given_keys.some((key) => !field_names.includes(key))) {
|
if (unknown_keys.length > 0) {
|
||||||
if (explain) {
|
if (options?.explain) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
`Entity "${this.name}" data must only contain known keys, got: "${given_keys}"`
|
`Entity "${this.name}" data must only contain known keys, unknown: "${unknown_keys}"`
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for (const field of fields) {
|
for (const field of fields) {
|
||||||
if (!field.isValid(data[field.name], context)) {
|
if (!field.isValid(data[field.name], context)) {
|
||||||
console.log("Entity.isValidData:invalid", context, field.name, data[field.name]);
|
console.log("Entity.isValidData:invalid", context, field.name, data[field.name]);
|
||||||
if (explain) {
|
if (options?.explain) {
|
||||||
throw new Error(`Field "${field.name}" has invalid data: "${data[field.name]}"`);
|
throw new Error(`Field "${field.name}" has invalid data: "${data[field.name]}"`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -29,7 +29,11 @@ export function useEntityForm({
|
|||||||
onSubmitAsync: async ({ value }): Promise<any> => {
|
onSubmitAsync: async ({ value }): Promise<any> => {
|
||||||
try {
|
try {
|
||||||
//console.log("validating", value, entity.isValidData(value, action));
|
//console.log("validating", value, entity.isValidData(value, action));
|
||||||
entity.isValidData(value, action, true);
|
entity.isValidData(value, action, {
|
||||||
|
explain: true,
|
||||||
|
// unknown will later be removed in getChangeSet
|
||||||
|
ignoreUnknown: true
|
||||||
|
});
|
||||||
return undefined;
|
return undefined;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
//console.log("---validation error", e);
|
//console.log("---validation error", e);
|
||||||
|
|||||||
Reference in New Issue
Block a user