Skip to content

Commit da632b5

Browse files
committed
fix: trim strings in data import fix: validation for choices field
1 parent a969761 commit da632b5

File tree

5 files changed

+40
-10
lines changed

5 files changed

+40
-10
lines changed

deno.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@inspatial/cloud",
3-
"version": "0.7.16",
3+
"version": "0.7.17",
44
"license": "Apache-2.0",
55
"exports": {
66
".": "./mod.ts",

src/data-import/csv-utils.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,12 @@ function sanitizeValue(str: string) {
8383
// Step 1: Remove single double quotes
8484
let cleanedStr = str.replace(/(?<!")"(?!")/g, "");
8585
// Step 2: Replace double double quotes with single quotes
86-
cleanedStr = cleanedStr.replace(/""/g, '"');
86+
cleanedStr = cleanedStr.replace(/""/g, '"').trim();
87+
88+
// If the string is empty, return null
89+
if (cleanedStr === "") {
90+
return null;
91+
}
8792

8893
// Step 3: Check if the string is a JSON object and parse it
8994
if (/^\{.*\}$/.test(cleanedStr)) {

src/orm/build/make-fields.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,20 @@ export function makeFields<ForType extends keyof ForTypeMap>(
5757
to: value,
5858
});
5959
const isValid = fieldType.validate(value, fieldDef);
60-
if (!isValid) {
61-
raiseORMException(
62-
`${value} is not a valid value for field ${field.key} in ${forType} ${instance._name}`,
63-
);
60+
if (isValid === true) {
61+
instance._data.set(field.key, value);
62+
return;
6463
}
65-
66-
instance._data.set(field.key, value);
64+
let message = "";
65+
if (typeof isValid === "string") {
66+
message = `: ${isValid}`;
67+
}
68+
message =
69+
`${value} is not a valid value for field ${field.key} in ${forType} ${instance._name}${message}`;
70+
raiseORMException(
71+
message,
72+
"InvalidValue",
73+
);
6774
},
6875
});
6976
}

src/orm/field/fields/choices-field.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,27 @@ export default new ORMFieldConfig("ChoicesField", {
1010
dbLoad(value, _fieldDef) {
1111
return value;
1212
},
13-
validate(_value, _fieldDef) {
13+
validate(value, fieldDef) {
14+
const choiceKeys = new Set(fieldDef.choices.map((c) => c.key));
15+
if (value === null || value === undefined) {
16+
return true;
17+
}
18+
if (typeof value !== "string") {
19+
return false;
20+
}
21+
if (!choiceKeys.has(value)) {
22+
return `Value "${value}" is not a valid choice. Valid choices are: ${
23+
Array.from(choiceKeys).join(", ")
24+
}`;
25+
}
1426
return true;
1527
},
28+
normalize(value, _fieldDef) {
29+
if (typeof value === "string") {
30+
return value.trim();
31+
}
32+
return value;
33+
},
1634
dbSave(value, _fieldDef) {
1735
return value;
1836
},

src/orm/field/orm-field.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export class ORMFieldConfig<T extends InFieldType = InFieldType> {
2828
* before saving it to the database.
2929
*/
3030

31-
validate: (value: any, fieldDef: InFieldMap[T]) => boolean;
31+
validate: (value: any, fieldDef: InFieldMap[T]) => boolean | string;
3232

3333
/**
3434
* A normalizer function that will be used to convert the field

0 commit comments

Comments
 (0)