Skip to content

Commit 91cc303

Browse files
js-tasandrina-p
authored andcommitted
Support integer values in select element
1 parent 3c2e9fc commit 91cc303

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed

src/tests/createHeadlessForm.test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ import {
6363
schemaForErrorMessageSpecificity,
6464
jsfConfigForErrorMessageSpecificity,
6565
schemaInputTypeFile,
66+
schemaWithSelectInteger,
6667
} from './helpers';
6768
import { mockConsole, restoreConsoleAndEnsureItWasNotCalled } from './testUtils';
6869
import { createHeadlessForm } from '@/createHeadlessForm';
@@ -657,6 +658,27 @@ describe('createHeadlessForm', () => {
657658
],
658659
});
659660
});
661+
662+
it('supports "select" field with integer values', async () => {
663+
const { handleValidation } = createHeadlessForm(schemaWithSelectInteger);
664+
const validateForm = (vals) => friendlyError(handleValidation(vals));
665+
666+
// Check for invalid option
667+
expect(
668+
validateForm({
669+
account_type: 4,
670+
})
671+
).toEqual({
672+
account_type: 'The option 4 is not valid.',
673+
});
674+
675+
// Check for valid option
676+
expect(
677+
validateForm({
678+
account_type: 1,
679+
})
680+
).toBeUndefined();
681+
});
660682
});
661683
describe('support "radio" field type', () => {
662684
it('support "radio" field type @deprecated', () => {

src/tests/helpers.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2327,3 +2327,24 @@ export const schemaWithCustomValidationsAndConditionals = {
23272327
},
23282328
],
23292329
};
2330+
2331+
export const schemaWithSelectInteger = {
2332+
additionalProperties: false,
2333+
type: 'object',
2334+
properties: {
2335+
account_type: {
2336+
title: 'Select an account type',
2337+
description: '',
2338+
'x-jsf-presentation': {
2339+
inputType: 'select',
2340+
},
2341+
oneOf: [
2342+
{ title: 'Normal User', const: 0 },
2343+
{ title: 'Business', const: 1 },
2344+
{ title: 'Services Provider', const: 2 },
2345+
],
2346+
type: 'integer',
2347+
},
2348+
},
2349+
required: [],
2350+
};

src/yupSchema.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,8 @@ const getYupSchema = ({ inputType, ...field }) => {
273273
switch (type) {
274274
case 'number':
275275
return yupSchemas.radioOrSelectNumber(optionValues);
276+
case 'integer':
277+
return yupSchemas.radioOrSelectNumber(optionValues);
276278
case 'boolean':
277279
return yupSchemas.radioOrSelectBoolean(optionValues);
278280
default:
@@ -513,7 +515,8 @@ export function buildYupSchema(field, config, logic) {
513515
validators.push(withFile);
514516
}
515517

516-
if (jsonType === 'integer') {
518+
const hasOptions = field.options?.length > 0;
519+
if (jsonType === 'integer' && !hasOptions) {
517520
validators.push(withInteger);
518521
}
519522

0 commit comments

Comments
 (0)