Skip to content

fix(select): Support integer values #142

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@remoteoss/json-schema-form",
"version": "0.11.11-beta.0",
"version": "0.11.12-dev.20250313084704",
Copy link
Collaborator

Choose a reason for hiding this comment

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

Hi @js-ta! We are building the next major version of JSF, which will include your bugfix/feature. For that reason we won't review your PR, so we can focus on shipping the next version as soon as possible.

In the meantime, here's a dev release based on this PR to unblock you:

npm i @remoteoss/json-schema-form@0.11.12-dev.20250313084704

We hope that's okay for now. Thank you!

"description": "Headless UI form powered by JSON Schemas",
"author": "Remote.com <engineering@remote.com> (https://remote.com/)",
"license": "MIT",
22 changes: 22 additions & 0 deletions src/tests/createHeadlessForm.test.js
Original file line number Diff line number Diff line change
@@ -63,6 +63,7 @@ import {
schemaForErrorMessageSpecificity,
jsfConfigForErrorMessageSpecificity,
schemaInputTypeFile,
schemaWithSelectInteger,
} from './helpers';
import { mockConsole, restoreConsoleAndEnsureItWasNotCalled } from './testUtils';
import { createHeadlessForm } from '@/createHeadlessForm';
@@ -657,6 +658,27 @@ describe('createHeadlessForm', () => {
],
});
});

it('supports "select" field with integer values', async () => {
const { handleValidation } = createHeadlessForm(schemaWithSelectInteger);
const validateForm = (vals) => friendlyError(handleValidation(vals));

// Check for invalid option
expect(
validateForm({
account_type: 4,
})
).toEqual({
account_type: 'The option 4 is not valid.',
});

// Check for valid option
expect(
validateForm({
account_type: 1,
})
).toBeUndefined();
});
});
describe('support "radio" field type', () => {
it('support "radio" field type @deprecated', () => {
21 changes: 21 additions & 0 deletions src/tests/helpers.js
Original file line number Diff line number Diff line change
@@ -2327,3 +2327,24 @@ export const schemaWithCustomValidationsAndConditionals = {
},
],
};

export const schemaWithSelectInteger = {
additionalProperties: false,
type: 'object',
properties: {
account_type: {
title: 'Select an account type',
description: '',
'x-jsf-presentation': {
inputType: 'select',
},
oneOf: [
{ title: 'Normal User', const: 0 },
{ title: 'Business', const: 1 },
{ title: 'Services Provider', const: 2 },
],
type: 'integer',
},
},
required: [],
};
5 changes: 4 additions & 1 deletion src/yupSchema.js
Original file line number Diff line number Diff line change
@@ -273,6 +273,8 @@ const getYupSchema = ({ inputType, ...field }) => {
switch (type) {
case 'number':
return yupSchemas.radioOrSelectNumber(optionValues);
case 'integer':
return yupSchemas.radioOrSelectNumber(optionValues);
case 'boolean':
return yupSchemas.radioOrSelectBoolean(optionValues);
default:
@@ -513,7 +515,8 @@ export function buildYupSchema(field, config, logic) {
validators.push(withFile);
}

if (jsonType === 'integer') {
const hasOptions = field.options?.length > 0;
if (jsonType === 'integer' && !hasOptions) {
validators.push(withInteger);
}

Loading
Oops, something went wrong.