-
Notifications
You must be signed in to change notification settings - Fork 437
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add range error validation for datatypes #1594
Merged
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
671c1d1
chore: add range error validation for date types
MichaelSun90 75118e0
add validation tests for numbers
mShan0 6effc24
remove only
mShan0 3074459
add money validation
mShan0 1058de9
Merge branch 'master' into michael-DatatypeRangeCheck
MichaelSun90 772340e
chore: remove un-needed types
MichaelSun90 8598f04
Merge branch 'michael-DatatypeRangeCheck' of https://github.com/tedio…
MichaelSun90 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
@@ -1,12 +1,18 @@ | ||
import { type DataType } from '../data-type'; | ||
import { ChronoUnit, LocalDate } from '@js-joda/core'; | ||
import { type InternalConnectionOptions } from '../connection'; | ||
|
||
import { Collation } from '../collation'; | ||
|
||
// globalDate is to be used for JavaScript's global 'Date' object to avoid name clashing with the 'Date' constant below | ||
const globalDate = global.Date; | ||
const EPOCH_DATE = LocalDate.ofYearDay(1, 1); | ||
const NULL_LENGTH = Buffer.from([0x00]); | ||
const DATA_LENGTH = Buffer.from([0x03]); | ||
|
||
const MIN_DATE = new globalDate('January 1, 0001'); | ||
const MAX_DATE = new globalDate('December 31, 9999'); | ||
|
||
const Date: DataType = { | ||
id: 0x28, | ||
type: 'DATEN', | ||
|
@@ -35,7 +41,7 @@ const Date: DataType = { | |
|
||
const value = parameter.value as any; // Temporary solution. Remove 'any' later. | ||
|
||
let date; | ||
let date: LocalDate; | ||
if (options.useUTC) { | ||
date = LocalDate.of(value.getUTCFullYear(), value.getUTCMonth() + 1, value.getUTCDate()); | ||
} else { | ||
|
@@ -49,7 +55,7 @@ const Date: DataType = { | |
}, | ||
|
||
// TODO: value is technically of type 'unknown'. | ||
validate: function(value): null | Date { | ||
validate: function(value: any, collation: Collation | undefined, options: InternalConnectionOptions): null | Date { | ||
if (value == null) { | ||
return null; | ||
} | ||
|
@@ -58,6 +64,18 @@ const Date: DataType = { | |
value = new globalDate(globalDate.parse(value)); | ||
} | ||
|
||
value = value as Date; | ||
|
||
// TODO: check date range: January 1, 0001, through December 31, 9999 | ||
// : time range: 00:00:00 through 23:59:59.997 | ||
if (options && options.useUTC) { | ||
value = new globalDate(value.toUTCString()); | ||
} | ||
|
||
if (value < MIN_DATE || value > MAX_DATE) { | ||
throw new TypeError('Out of range.'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this be |
||
} | ||
|
||
if (isNaN(value)) { | ||
throw new TypeError('Invalid date.'); | ||
} | ||
|
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
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
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
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
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this work? 🤔