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

feat: ignore minimum check when a number field is empty #95

Merged
merged 3 commits into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,6 @@ test('Form/hooks/DynamicField', () => {

const errors = {
name: false,
'name.id': ErrorMessages.minNumber(10),
'name.name': ErrorMessages.REQUIRED,
'name.description': ErrorMessages.REQUIRED,
'name.settings': ErrorMessages.REQUIRED,
Expand Down Expand Up @@ -213,7 +212,6 @@ test('Form/hooks/DynamicField', () => {

const errors1 = {
name: false,
'name.id': ErrorMessages.minNumber(10),
'name.name': ErrorMessages.REQUIRED,
'name.description': ErrorMessages.REQUIRED,
'name.settings': ErrorMessages.REQUIRED,
Expand Down Expand Up @@ -250,7 +248,6 @@ test('Form/hooks/DynamicField', () => {

const errors2 = {
name: false,
'name.id': ErrorMessages.minNumber(10),
'name.name': ErrorMessages.REQUIRED,
'name.description': ErrorMessages.REQUIRED,
'name.settings': ErrorMessages.REQUIRED,
Expand Down Expand Up @@ -322,7 +319,7 @@ test('Form/hooks/DynamicField', () => {

const errors4 = {
name: false,
'name.id': ErrorMessages.minNumber(10),
'name.id': false,
'name.name': ErrorMessages.REQUIRED,
'name.description': ErrorMessages.REQUIRED,
'name.settings': ErrorMessages.REQUIRED,
Expand Down Expand Up @@ -372,7 +369,7 @@ test('Form/hooks/DynamicField', () => {

const errors5 = {
name: false,
'name.id': ErrorMessages.minNumber(10),
'name.id': false,
'name.name': ErrorMessages.REQUIRED,
'name.description': ErrorMessages.REQUIRED,
'name.settings': ErrorMessages.REQUIRED,
Expand Down
1 change: 1 addition & 0 deletions src/lib/kit/validators/__tests__/validators.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ describe('kit/validators/validators', () => {
spec.minimum = 100.5;

expect(getNumberValidator({ignoreMinimumCheck: true})(spec, '1.01')).toBe(false);
expect(validator(spec, '')).toBe(false);
expect(validator(spec, '1.01')).toBe(ErrorMessages.minNumber(spec.minimum));
expect(validator(spec, 1.01)).toBe(ErrorMessages.minNumber(spec.minimum));
expect(validator(spec, 101)).toBe(false);
Expand Down
3 changes: 2 additions & 1 deletion src/lib/kit/validators/validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@ export const getNumberValidator = (params: GetNumberValidatorParams = {}) => {
if (
!ignoreMinimumCheck &&
_.isNumber(spec.minimum) &&
((stringValue.length && spec.minimum > Number(stringValue)) || !stringValue.length)
stringValue.length &&
spec.minimum > Number(stringValue)
) {
return ErrorMessages.minNumber(spec.minimum);
}
Expand Down