Forbid default value nullification for non-nullable field (#6258)

as per title 


https://github.com/user-attachments/assets/ce07d437-eeeb-488c-8dfa-3fc07316f485
This commit is contained in:
Marie
2024-07-15 14:22:15 +02:00
committed by GitHub
parent d560d25736
commit aed0bf41ce
3 changed files with 63 additions and 0 deletions

View File

@ -0,0 +1,38 @@
import { assertDoesNotNullifyDefaultValueForNonNullableField } from 'src/engine/metadata-modules/field-metadata/utils/assert-does-not-nullify-default-value-for-non-nullable-field.util';
describe('assertDoesNotNullifyDefaultValueForNonNullableField', () => {
it('should not throw if default value is set to null and field is nullable', () => {
expect(() =>
assertDoesNotNullifyDefaultValueForNonNullableField({
isNullable: true,
defaultValueFromUpdate: null,
}),
).not.toThrow();
});
it('should not throw if default value is undefined and field is non nullable', () => {
expect(() =>
assertDoesNotNullifyDefaultValueForNonNullableField({
isNullable: false,
}),
).not.toThrow();
});
it('should not throw if default value is not set to null and field is non nullable', () => {
expect(() =>
assertDoesNotNullifyDefaultValueForNonNullableField({
isNullable: false,
defaultValueFromUpdate: 'new default value',
}),
).not.toThrow();
});
it('should throw if default value is set to null and field is non nullable', () => {
expect(() =>
assertDoesNotNullifyDefaultValueForNonNullableField({
isNullable: false,
defaultValueFromUpdate: null,
}),
).toThrow();
});
});

View File

@ -0,0 +1,19 @@
import {
FieldMetadataException,
FieldMetadataExceptionCode,
} from 'src/engine/metadata-modules/field-metadata/field-metadata.exception';
export const assertDoesNotNullifyDefaultValueForNonNullableField = ({
isNullable,
defaultValueFromUpdate,
}: {
isNullable: boolean;
defaultValueFromUpdate?: any;
}) => {
if (!isNullable && defaultValueFromUpdate === null) {
throw new FieldMetadataException(
'Default value cannot be nullified for non-nullable field',
FieldMetadataExceptionCode.INVALID_FIELD_INPUT,
);
}
};