Skip to content

Commit

Permalink
feat: deprecate automatic type coercion from objects to strings
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurschreiber authored Sep 30, 2021
1 parent 888cde1 commit 6ff0e1e
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 1 deletion.
17 changes: 17 additions & 0 deletions src/data-types/char.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ const Char: { maximumLength: number } & DataType = {
if (typeof value.toString !== 'function') {
throw new TypeError('Invalid string.');
}

emitTypeCoercionWarning();
value = value.toString();
}

Expand All @@ -101,3 +103,18 @@ const Char: { maximumLength: number } & DataType = {

export default Char;
module.exports = Char;

let typeCoercionWarningEmitted = false;
function emitTypeCoercionWarning() {
if (typeCoercionWarningEmitted) {
return;
}

typeCoercionWarningEmitted = true;

process.emitWarning(
'`char` type coercion from non-string type value via `.toString()` method is deprecated and will be removed.',
'DeprecationWarning',
Char.validate
);
}
17 changes: 17 additions & 0 deletions src/data-types/nchar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ const NChar: DataType & { maximumLength: number } = {
if (typeof value.toString !== 'function') {
throw new TypeError('Invalid string.');
}

emitTypeCoercionWarning();
value = value.toString();
}
return value;
Expand All @@ -110,3 +112,18 @@ const NChar: DataType & { maximumLength: number } = {

export default NChar;
module.exports = NChar;

let typeCoercionWarningEmitted = false;
function emitTypeCoercionWarning() {
if (typeCoercionWarningEmitted) {
return;
}

typeCoercionWarningEmitted = true;

process.emitWarning(
'`nchar` type coercion from non-string type value via `.toString()` method is deprecated and will be removed.',
'DeprecationWarning',
NChar.validate
);
}
17 changes: 17 additions & 0 deletions src/data-types/ntext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ const NText: DataType = {
if (typeof value.toString !== 'function') {
throw new TypeError('Invalid string.');
}

emitTypeCoercionWarning();
value = value.toString();
}

Expand All @@ -71,3 +73,18 @@ const NText: DataType = {

export default NText;
module.exports = NText;

let typeCoercionWarningEmitted = false;
function emitTypeCoercionWarning() {
if (typeCoercionWarningEmitted) {
return;
}

typeCoercionWarningEmitted = true;

process.emitWarning(
'`ntext` type coercion from non-string type value via `.toString()` method is deprecated and will be removed.',
'DeprecationWarning',
NText.validate
);
}
17 changes: 17 additions & 0 deletions src/data-types/nvarchar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ const NVarChar: { maximumLength: number } & DataType = {
if (typeof value.toString !== 'function') {
throw new TypeError('Invalid string.');
}

emitTypeCoercionWarning();
value = value.toString();
}
return value;
Expand All @@ -149,3 +151,18 @@ const NVarChar: { maximumLength: number } & DataType = {

export default NVarChar;
module.exports = NVarChar;

let typeCoercionWarningEmitted = false;
function emitTypeCoercionWarning() {
if (typeCoercionWarningEmitted) {
return;
}

typeCoercionWarningEmitted = true;

process.emitWarning(
'`nvarchar` type coercion from non-string type value via `.toString()` method is deprecated and will be removed.',
'DeprecationWarning',
NVarChar.validate
);
}
18 changes: 17 additions & 1 deletion src/data-types/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ const Text: DataType = {
if (typeof value.toString !== 'function') {
throw new TypeError('Invalid string.');
}

emitTypeCoercionWarning();
value = value.toString();
}

Expand All @@ -83,6 +85,20 @@ const Text: DataType = {
}
};


export default Text;
module.exports = Text;

let typeCoercionWarningEmitted = false;
function emitTypeCoercionWarning() {
if (typeCoercionWarningEmitted) {
return;
}

typeCoercionWarningEmitted = true;

process.emitWarning(
'`text` type coercion from non-string type value via `.toString()` method is deprecated and will be removed.',
'DeprecationWarning',
Text.validate
);
}
16 changes: 16 additions & 0 deletions src/data-types/uniqueidentifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const UniqueIdentifier: DataType = {
throw new TypeError('Invalid string.');
}

emitTypeCoercionWarning();
value = value.toString();
}

Expand All @@ -60,3 +61,18 @@ const UniqueIdentifier: DataType = {

export default UniqueIdentifier;
module.exports = UniqueIdentifier;

let typeCoercionWarningEmitted = false;
function emitTypeCoercionWarning() {
if (typeCoercionWarningEmitted) {
return;
}

typeCoercionWarningEmitted = true;

process.emitWarning(
'`uniqueidentifier` type coercion from non-string type value via `.toString()` method is deprecated and will be removed.',
'DeprecationWarning',
UniqueIdentifier.validate
);
}
16 changes: 16 additions & 0 deletions src/data-types/varchar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ const VarChar: { maximumLength: number } & DataType = {
throw new TypeError('Invalid string.');
}

emitTypeCoercionWarning();
value = value.toString();
}

Expand All @@ -134,3 +135,18 @@ const VarChar: { maximumLength: number } & DataType = {

export default VarChar;
module.exports = VarChar;

let typeCoercionWarningEmitted = false;
function emitTypeCoercionWarning() {
if (typeCoercionWarningEmitted) {
return;
}

typeCoercionWarningEmitted = true;

process.emitWarning(
'`varchar` type coercion from non-string type value via `.toString()` method is deprecated and will be removed.',
'DeprecationWarning',
VarChar.validate
);
}

0 comments on commit 6ff0e1e

Please sign in to comment.