Skip to content

Commit

Permalink
🐛 Don't cast empty strings to 0 values in numeric Field types
Browse files Browse the repository at this point in the history
  • Loading branch information
skerit committed Feb 6, 2024
1 parent 1078ba2 commit 544fe9b
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* The `Alchemy.ClientSession` class should now be used as a map to store session data
* Fix `Field.Schema` contents not being converted from database representation when the schema also has a relation
* Make `Criteria#contains(value)` work as expected on array fields
* Don't cast empty strings to `0` values in numeric `Field` types

## 1.3.22 (2023-12-21)

Expand Down
12 changes: 7 additions & 5 deletions lib/app/helper_field/10-number_field.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
* @since 0.2.0
* @version 1.1.0
*/
var NumberField = Function.inherits('Alchemy.Field', function Number(schema, name, options) {
Number.super.call(this, schema, name, options);
});
const NumberField = Function.inherits('Alchemy.Field', 'Number');

/**
* Set the datatype name
Expand All @@ -25,14 +23,18 @@ NumberField.setDatatype('number');
*
* @author Jelle De Loecker <[email protected]>
* @since 0.2.0
* @version 0.4.1
* @version 1.4.0
*
* @param {Mixed} value
* @param {*} value
*
* @return {number}
*/
NumberField.setMethod(function cast(value) {

if (value === '') {
value = null;
}

// Allow null
if (value == null) {
return value;
Expand Down
6 changes: 5 additions & 1 deletion lib/app/helper_field/20-decimal_field.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,18 @@ const DecimalField = Function.inherits('Alchemy.Field.Number', 'Decimal');
*
* @author Jelle De Loecker <[email protected]>
* @since 1.3.20
* @version 1.3.20
* @version 1.4.0
*
* @param {Mixed} value
*
* @return {Decimal}
*/
DecimalField.setMethod(function cast(value) {

if (value === '') {
value = null;
}

// Allow null
if (value == null) {
return value;
Expand Down
6 changes: 5 additions & 1 deletion lib/app/helper_field/big_int_field.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,18 @@ BigIntField.setDatatype('bigint');
*
* @author Jelle De Loecker <[email protected]>
* @since 1.3.6
* @version 1.3.6
* @version 1.4.0
*
* @param {Mixed} value
*
* @return {BigInt}
*/
BigIntField.setMethod(function cast(value) {

if (value === '') {
value = null;
}

// Allow null
if (value == null) {
return value;
Expand Down
6 changes: 5 additions & 1 deletion lib/app/helper_field/fixed_decimal_field.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,18 @@ const FixedDecimalField = Function.inherits('Alchemy.Field.Decimal', function Fi
*
* @author Jelle De Loecker <[email protected]>
* @since 1.3.20
* @version 1.3.20
* @version 1.4.0
*
* @param {Mixed} value
*
* @return {Decimal}
*/
FixedDecimalField.setMethod(function cast(value) {

if (value === '') {
value = null;
}

// Allow null
if (value == null) {
return value;
Expand Down
12 changes: 7 additions & 5 deletions lib/app/helper_field/integer_field.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,25 @@
* @since 1.1.0
* @version 1.1.0
*/
var IntegerField = Function.inherits('Alchemy.Field.Number', function Integer(schema, name, options) {
Integer.super.call(this, schema, name, options);
});
const IntegerField = Function.inherits('Alchemy.Field.Number', 'Integer');

/**
* Cast the given value to this field's type
*
* @author Jelle De Loecker <[email protected]>
* @since 1.1.0
* @version 1.1.0
* @version 1.4.0
*
* @param {Mixed} value
* @param {*} value
*
* @return {number}
*/
IntegerField.setMethod(function cast(value) {

if (value === '') {
value = null;
}

// Allow null
if (value == null) {
return value;
Expand Down

0 comments on commit 544fe9b

Please sign in to comment.