-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Forbid default value nullification for non-nullable field (#6258)
as per title https://github.com/user-attachments/assets/ce07d437-eeeb-488c-8dfa-3fc07316f485
- Loading branch information
Showing
3 changed files
with
63 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
...data/utils/__tests__/assert-does-not-nullify-default-value-for-non-nullable-field.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |
19 changes: 19 additions & 0 deletions
19
...field-metadata/utils/assert-does-not-nullify-default-value-for-non-nullable-field.util.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
); | ||
} | ||
}; |