Skip to content
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

refactor: migrate decimal validator to ts #750

Merged
merged 5 commits into from
Dec 9, 2020

Conversation

tshuli
Copy link
Contributor

@tshuli tshuli commented Nov 30, 2020

To be merged in after #749

Problem

Part of #7

Solution

Improvements

  • Migrates Decimal validation to Typescript

Tests

  • Create two decimal fields. Hide one of them behind logic.
  • Check that validation on both visible and hidden decimal fields work when submitting programatically
  • Decimal input is rejected if it is not within the validation range
  • Decimal input is rejected if it has leading zeroes

@karrui
Copy link
Contributor

karrui commented Nov 30, 2020

This PR seems to have both the dateValidator and decimalValidator changes, can help to separate them first before I review?

@liangyuanruo liangyuanruo changed the base branch from develop to refactor/date-validator December 1, 2020 06:59
Comment on lines 22 to 25
interface IIsFloatOptions {
min?: number
max?: number
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't declare interfaces in a running function

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shifted out

const makeDecimalFloatRangeValidator: DecimalValidatorConstructor = (
decimalField,
) => (response) => {
const { customMin, customMax } = decimalField.ValidationOptions || {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is the fallback to empty object truly necessary?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't be since customMin and customMax both have defaults, but we should also double check (eg by deleting ValidationOptions on a decimal field and checking if mongoose puts it back automatically)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed the fallback

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@liangyuanruo @mantariksh actually can we keep the fallback since there is really no downside; although mongoose restores the default when I tested it manually, the failing tests make me nervous

Comment on lines 27 to 37
const isFloatOptions: IIsFloatOptions = {}
if (customMin || customMin === 0) {
isFloatOptions['min'] = customMin
}
if (customMax || customMax === 0) {
isFloatOptions['max'] = customMax
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would this end up achieving the same logic?

const isFloatOptions = {
  min: customMin,
  max: customMax
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IsFloatOptions activates its options based on the presence of each property, hence it is necessary to manually add 'min' and 'max'; added a comment within

// isFloat validates range correctly for floats up to 15 decimal places
// (1.999999999999999 >= 2) is False
// (1.9999999999999999 >= 2) is True
return isFloat(answer, isFloatOptions)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe the isFloatOptions variable can be inlined into isFloat directly?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As above, no because the properties must be conditionally added

@tshuli tshuli force-pushed the refactor/date-validator branch 5 times, most recently from b1d8260 to c7befe4 Compare December 7, 2020 02:34
@tshuli tshuli force-pushed the refactor/decimal-validator branch from 1f2c852 to 56df4d6 Compare December 7, 2020 05:31
@tshuli tshuli force-pushed the refactor/date-validator branch from b7d21cd to 327744f Compare December 7, 2020 12:21
@tshuli tshuli force-pushed the refactor/decimal-validator branch from 72dfbc0 to 3bd0e54 Compare December 7, 2020 12:23
@tshuli tshuli changed the base branch from refactor/date-validator to develop December 8, 2020 12:11
@tshuli tshuli force-pushed the refactor/decimal-validator branch from 1254e92 to 92c76e3 Compare December 8, 2020 12:13
@tshuli tshuli force-pushed the refactor/decimal-validator branch from 92c76e3 to 381efaf Compare December 8, 2020 12:30
@tshuli tshuli merged commit f6a91e2 into develop Dec 9, 2020
@tshuli tshuli deleted the refactor/decimal-validator branch December 9, 2020 04:52
@mantariksh mantariksh mentioned this pull request Dec 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants