Skip to content
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

nc-fix/ SingleLineText Fields silently dropping larger than length values #8437

Merged
15 changes: 8 additions & 7 deletions packages/nocodb-sdk/src/lib/sqlUi/MysqlUi.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import UITypes from '../UITypes';
import { IDType } from './index';
import { IDType } from '~/lib';

const dbTypes = [
'int',
Expand Down Expand Up @@ -630,17 +630,13 @@ export class MysqlUi {

static colPropUNDisabled(col) {
// console.log(col);
if (
return !(
col.dt === 'int' ||
col.dt === 'tinyint' ||
col.dt === 'smallint' ||
col.dt === 'mediumint' ||
col.dt === 'bigint'
) {
return false;
} else {
return true;
}
);
}

static onCheckboxChangeAI(col) {
Expand Down Expand Up @@ -1015,6 +1011,11 @@ export class MysqlUi {
break;
case 'SingleLineText':
colProp.dt = 'varchar';
colProp.validate = {
func: ['isLength'],
args: [{ max: 45 }], // Assuming 45 is the max length for SingleLineText
msg: ['Validation failed : isLength ({cn})'],
};
break;
case 'LongText':
colProp.dt = 'text';
Expand Down
16 changes: 16 additions & 0 deletions packages/nocodb/src/db/BaseModelSqlv2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4477,6 +4477,8 @@ class BaseModelSqlv2 {
);
}
await this.validateOptions(column, data);
// Validates the constraints on the data based on the column definitions
this.validateConstraints(column, data);
rohittp0 marked this conversation as resolved.
Show resolved Hide resolved

// skip validation if `validate` is undefined or false
if (!column?.meta?.validate || !column?.validate) continue;
Expand Down Expand Up @@ -4512,6 +4514,20 @@ class BaseModelSqlv2 {
return true;
}

/*
* Utility method to validate database constraints
*/
protected validateConstraints(
column: Column<any>,
data: Record<string, any>,
) {
if (column.clen < data[column.title]?.length) {
pranavxc marked this conversation as resolved.
Show resolved Hide resolved
NcError.badRequest(
`Column "${column.title}" value exceeds the maximum length of ${column.clen}`,
);
}
}
rohittp0 marked this conversation as resolved.
Show resolved Hide resolved

rohittp0 marked this conversation as resolved.
Show resolved Hide resolved
// method for validating otpions if column is single/multi select
protected async validateOptions(
column: Column<any>,
Expand Down
Loading
Loading