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

fix: support maximum 0 in number field #10

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
24 changes: 24 additions & 0 deletions src/tests/createHeadlessForm.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
schemaInputTypeSelectMultiple,
schemaInputTypeSelectMultipleOptional,
schemaInputTypeNumber,
schemaInputTypeNumberZeroMaximum,
schemaInputTypeDate,
schemaInputTypeEmail,
schemaInputWithStatement,
Expand Down Expand Up @@ -780,6 +781,29 @@ describe('createHeadlessForm', () => {
expect(() => fieldValidator.validateSync('')).toThrowError('The value must be a number');
});

it('shows an error when positive number is entered into a "number" field with maximum set to zero', () => {
jguddas marked this conversation as resolved.
Show resolved Hide resolved
const result = createHeadlessForm(schemaInputTypeNumberZeroMaximum);
expect(result).toMatchObject({
fields: [
{
description: 'How many open tabs do you have?',
label: 'Tabs',
name: 'tabs',
required: false,
schema: expect.any(Object),
type: 'number',
Copy link
Collaborator

Choose a reason for hiding this comment

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

todo: type is deprecated. Instead, can you assert jsonType and inputType?

minimum: -100,
maximum: 0,
},
],
});
const fieldValidator = result.fields[0].schema;
expect(fieldValidator.isValidSync('0')).toBe(true);
expect(fieldValidator.isValidSync('10')).toBe(false);
expect(fieldValidator.isValidSync('-11')).toBe(true);
expect(() => fieldValidator.validateSync('5')).toThrowError('Must be smaller or equal to 0');
});

it('support "number" field type with the percentage attribute', () => {
const result = createHeadlessForm(schemaInputTypeNumberWithPercentage);
expect(result).toMatchObject({
Expand Down
17 changes: 17 additions & 0 deletions src/tests/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,23 @@ export const mockNumberInput = {
type: 'number',
};

export const mockNumberInputNegative = {
title: 'Tabs',
description: 'How many open tabs do you have?',
'x-jsf-presentation': {
inputType: 'number',
},
minimum: -100,
maximum: 0,
type: 'number',
};

export const schemaInputTypeNumberZeroMaximum = JSONSchemaBuilder()
jguddas marked this conversation as resolved.
Show resolved Hide resolved
.addInput({
tabs: mockNumberInputNegative,
})
.build();

export const mockNumberInputWithPercentage = {
title: 'Shares',
description: 'What % of shares do you own?',
Expand Down
3 changes: 2 additions & 1 deletion src/yupSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,8 @@ export function buildYupSchema(field, config) {
validators.push(withMinLength);
}

if (propertyFields.maximum) {
// support maximum with 0 value
if (propertyFields.maximum !== undefined) {
validators.push(withMax);
}

Expand Down