Skip to content

fix: fixed sending strings instead of integers error #2216

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions src/lib/elements/forms/inputNumber.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,19 @@

const parsed = Number(raw);
if (Number.isFinite(parsed)) {
value = parsed;
if (step === 1) {
if (!Number.isInteger(parsed)) {
const lowerInt = Math.floor(parsed);
const upperInt = Math.ceil(parsed);
error = `Value must be an integer. Please enter a value between ${lowerInt} and ${upperInt}.`;
return;
}
value = parsed;
error = null;
} else {
value = parsed;
error = null;
}
}
}

Expand All @@ -52,7 +64,15 @@
error = event.currentTarget.validationMessage;
};

$: if (value !== null && value !== undefined && !Number.isNaN(value)) {
$: if (
step === 1 &&
value !== null &&
value !== undefined &&
!Number.isNaN(value) &&
Number.isInteger(value)
) {
error = null;
} else if (step !== 1 && value !== null && value !== undefined && !Number.isNaN(value)) {
error = null;
}
</script>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,23 @@
key: string,
data: Partial<Models.AttributeInteger>
) {
const min = data.min !== null && data.min !== undefined ? Math.round(data.min) : undefined;
const max = data.max !== null && data.max !== undefined ? Math.round(data.max) : undefined;
const defaultValue =
data.default !== null && data.default !== undefined
? Math.round(data.default)
: undefined;

await sdk
.forProject(page.params.region, page.params.project)
.databases.createIntegerAttribute(
databaseId,
collectionId,
key,
data.required,
data.min,
data.max,
data.default,
min,
max,
defaultValue,
data.array
);
}
Expand All @@ -29,16 +36,23 @@
data: Partial<Models.AttributeInteger>,
originalKey?: string
) {
const min = data.min !== null && data.min !== undefined ? Math.round(data.min) : data.min;
const max = data.max !== null && data.max !== undefined ? Math.round(data.max) : data.max;
const defaultValue =
data.default !== null && data.default !== undefined
? Math.round(data.default)
: data.default;

await sdk
.forProject(page.params.region, page.params.project)
.databases.updateIntegerAttribute(
databaseId,
collectionId,
originalKey,
data.required,
data.default,
Math.abs(data.min) > Number.MAX_SAFE_INTEGER ? undefined : data.min,
Math.abs(data.max) > Number.MAX_SAFE_INTEGER ? undefined : data.max,
defaultValue,
Math.abs(min) > Number.MAX_SAFE_INTEGER ? undefined : min,
Math.abs(max) > Number.MAX_SAFE_INTEGER ? undefined : max,
data.key !== originalKey ? data.key : undefined
);
}
Expand Down Expand Up @@ -90,12 +104,14 @@
label="Min"
placeholder="Enter size"
bind:value={data.min}
step={1}
required={editing} />
<InputNumber
id="max"
label="Max"
placeholder="Enter size"
bind:value={data.max}
step={1}
required={editing} />
</Layout.Stack>
<InputNumber
Expand All @@ -105,6 +121,7 @@
min={data.min}
max={data.max}
bind:value={data.default}
step={1}
disabled={data.required || data.array}
nullable={!data.required && !data.array} />
<Selector.Checkbox
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,42 @@

async function create() {
try {
const processedDocument = { ...$createDocument.document } as Record<string, unknown>;
for (const attr of $createDocument.attributes) {
if (attr.type === 'integer') {
const key = attr.key as keyof typeof processedDocument;
const val = processedDocument[key] as unknown;
if (attr.array && Array.isArray(val)) {
processedDocument[key] = (val as unknown[]).map((v) => {
if (typeof v === 'number' && Number.isFinite(v))
return Math.round(v as number);
if (
typeof v === 'string' &&
v.trim() !== '' &&
Number.isFinite(Number(v))
)
return Math.round(Number(v));
return v;
});
} else if (typeof val === 'number' && Number.isFinite(val)) {
processedDocument[key] = Math.round(val as number);
} else if (
typeof val === 'string' &&
val.trim() !== '' &&
Number.isFinite(Number(val))
) {
processedDocument[key] = Math.round(Number(val));
}
}
}

const { $id } = await sdk
.forProject(page.params.region, page.params.project)
.databases.createDocument(
page.params.database,
page.params.collection,
$createDocument.id ?? ID.unique(),
$createDocument.document,
processedDocument,
$createDocument.permissions
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,41 @@

async function updateData() {
try {
const processedWork = { ...$work } as Record<string, unknown>;
for (const attr of $collection.attributes) {
if (attr.type === 'integer') {
const key = attr.key as keyof typeof processedWork;
const val = processedWork[key] as unknown;
if (attr.array && Array.isArray(val)) {
processedWork[key] = (val as unknown[]).map((v) => {
if (typeof v === 'number' && Number.isFinite(v))
return Math.round(v as number);
if (
typeof v === 'string' &&
v.trim() !== '' &&
Number.isFinite(Number(v))
)
return Math.round(Number(v));
return v;
});
} else if (typeof val === 'number' && Number.isFinite(val)) {
processedWork[key] = Math.round(val as number);
} else if (
typeof val === 'string' &&
val.trim() !== '' &&
Number.isFinite(Number(val))
) {
processedWork[key] = Math.round(Number(val));
}
}
}
await sdk
.forProject(page.params.region, page.params.project)
.databases.updateDocument(
databaseId,
collectionId,
documentId,
$work,
processedWork,
$work.$permissions
);
await invalidate(Dependencies.DOCUMENT);
Expand Down