mirror of
https://github.com/nocobase/nocobase.git
synced 2026-09-01 14:57:36 +08:00
fix(database): date validation (#8867)
This commit is contained in:
@@ -129,6 +129,16 @@ describe('validation', () => {
|
||||
).rejects.toThrow();
|
||||
});
|
||||
|
||||
it('should throw validation error when precision exceeds limit with string input', async () => {
|
||||
await expect(
|
||||
NumberCollection.repository.create({
|
||||
values: {
|
||||
amount: '1.234',
|
||||
},
|
||||
}),
|
||||
).rejects.toThrow();
|
||||
});
|
||||
|
||||
it('should succeed when precision is within limit', async () => {
|
||||
const result = await NumberCollection.repository.create({
|
||||
values: {
|
||||
@@ -140,6 +150,39 @@ describe('validation', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('date field validation', () => {
|
||||
let DateCollection: Collection;
|
||||
|
||||
beforeEach(async () => {
|
||||
DateCollection = db.collection({
|
||||
name: 'dates',
|
||||
fields: [
|
||||
{
|
||||
type: 'date',
|
||||
name: 'scheduledAt',
|
||||
allowNull: true,
|
||||
validation: {
|
||||
type: 'date',
|
||||
rules: [{ key: `r_${uid()}`, name: 'required' }],
|
||||
},
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
await db.sync();
|
||||
});
|
||||
|
||||
it('should accept date string input when validation type is date', async () => {
|
||||
const result = await DateCollection.repository.create({
|
||||
values: {
|
||||
scheduledAt: '2026-03-13 10:00:00',
|
||||
},
|
||||
});
|
||||
|
||||
expect(result.get('scheduledAt')).toBeTruthy();
|
||||
});
|
||||
});
|
||||
|
||||
describe('association field validation', () => {
|
||||
let User: Collection;
|
||||
let Profile: Collection;
|
||||
|
||||
@@ -273,7 +273,7 @@ export class Collection<
|
||||
label: `${this.name}.${field.name}`,
|
||||
value: val,
|
||||
});
|
||||
const { error } = joiSchema.validate(val, { convert: false });
|
||||
const { error } = joiSchema.validate(val);
|
||||
if (error) {
|
||||
throw error;
|
||||
}
|
||||
|
||||
@@ -33,6 +33,10 @@ export function buildJoiSchema(validation: ValidationOptions, options: { label?:
|
||||
if (rules) {
|
||||
rules.forEach((rule) => {
|
||||
const args = _.cloneDeep(rule.args);
|
||||
if (rule.name === 'precision') {
|
||||
// Keep precision validation strict even when convert is enabled at validate-time.
|
||||
schema = schema.strict();
|
||||
}
|
||||
if (!_.isEmpty(args)) {
|
||||
if (rule.name === 'pattern' && !_.isRegExp(args.regex)) {
|
||||
const lastSlash = args.regex.lastIndexOf('/');
|
||||
|
||||
Reference in New Issue
Block a user