Skip to content

Commit

Permalink
Forbid default value nullification for non-nullable field (#6258)
Browse files Browse the repository at this point in the history
  • Loading branch information
ijreilly authored Jul 15, 2024
1 parent d560d25 commit aed0bf4
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
FieldMetadataException,
FieldMetadataExceptionCode,
} from 'src/engine/metadata-modules/field-metadata/field-metadata.exception';
import { assertDoesNotNullifyDefaultValueForNonNullableField } from 'src/engine/metadata-modules/field-metadata/utils/assert-does-not-nullify-default-value-for-non-nullable-field.util';
import { computeColumnName } from 'src/engine/metadata-modules/field-metadata/utils/compute-column-name.util';
import { generateNullable } from 'src/engine/metadata-modules/field-metadata/utils/generate-nullable';
import { ObjectMetadataService } from 'src/engine/metadata-modules/object-metadata/object-metadata.service';
Expand Down Expand Up @@ -310,6 +311,11 @@ export class FieldMetadataService extends TypeOrmQueryService<FieldMetadataEntit

assertMutationNotOnRemoteObject(objectMetadata);

assertDoesNotNullifyDefaultValueForNonNullableField({
isNullable: existingFieldMetadata.isNullable,
defaultValueFromUpdate: fieldMetadataInput.defaultValue,
});

if (
objectMetadata.labelIdentifierFieldMetadataId ===
existingFieldMetadata.id &&
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
});
});
Original file line number Diff line number Diff line change
@@ -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,
);
}
};

0 comments on commit aed0bf4

Please sign in to comment.