fix(database): date validation (#8867)

This commit is contained in:
YANG QIA
2026-03-13 21:44:31 +08:00
committed by GitHub
parent e90f164ad9
commit 61b472e85c
3 changed files with 48 additions and 1 deletions
@@ -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;
+1 -1
View File
@@ -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('/');