Skip to content

Commit 2162f50

Browse files
committed
Replaced numeric fields in bad input state with invalid value
1 parent 28dcc84 commit 2162f50

File tree

5 files changed

+12
-33
lines changed

5 files changed

+12
-33
lines changed

jschema/__tests__/bad_input_checker.test.js renamed to jschema/__tests__/bad_input.test.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { describe, it, expect, vi } from 'vitest';
22
import { FormManager } from '../src/lib/components/form_manager';
3-
import { checkBadInputs } from '../src/lib/components/bad_input_checker';
43

5-
describe('bad_input_checker', () => {
4+
describe('badInput', () => {
65
it('Detect bad input in nested object', () => {
76
const manager = new FormManager(
87
{
@@ -21,9 +20,9 @@ describe('bad_input_checker', () => {
2120
},
2221
vi.fn()
2322
);
24-
expect(() => checkBadInputs(manager.root)).not.toThrowError();
23+
expect(manager.getFormData()).deep.eq({ foo: { bar: null } });
2524
manager.root.children[0].children[0].badInput = true;
26-
expect(() => checkBadInputs(manager.root)).toThrowError('Invalid JSON Schema data');
25+
expect(manager.getFormData()).deep.eq({ foo: { bar: 'invalid' } });
2726
});
2827

2928
it('Detect bad input in nested array', () => {
@@ -43,9 +42,9 @@ describe('bad_input_checker', () => {
4342
},
4443
vi.fn()
4544
);
46-
expect(() => checkBadInputs(manager.root)).not.toThrowError();
45+
expect(manager.getFormData()).deep.eq({ foo: [0] });
4746
manager.root.children[0].children[0].badInput = true;
48-
expect(() => checkBadInputs(manager.root)).toThrowError('Invalid JSON Schema data');
47+
expect(manager.getFormData()).deep.eq({ foo: ['invalid'] });
4948
});
5049

5150
it('Detect bad input in nested tuple', () => {
@@ -69,8 +68,8 @@ describe('bad_input_checker', () => {
6968
},
7069
vi.fn()
7170
);
72-
expect(() => checkBadInputs(manager.root)).not.toThrowError();
71+
expect(manager.getFormData()).deep.eq({ foo: [0] });
7372
manager.root.children[0].children[0].badInput = true;
74-
expect(() => checkBadInputs(manager.root)).toThrowError('Invalid JSON Schema data');
73+
expect(manager.getFormData()).deep.eq({ foo: ['invalid'] });
7574
});
7675
});

jschema/src/lib/components/bad_input_checker.js

Lines changed: 0 additions & 21 deletions
This file was deleted.

jschema/src/lib/components/form_manager.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import {
1919
isTuple
2020
} from './property_utils.js';
2121
import { SchemaValidator } from './jschema_validation.js';
22-
import { checkBadInputs } from './bad_input_checker';
2322

2423
/**
2524
* Creates the object used to draw the JSON Schema form, provides the functions to initialize new form elements,
@@ -380,7 +379,6 @@ export class FormManager {
380379
const errors = this.validator.getErrors();
381380
throw new JsonSchemaDataError(errors);
382381
}
383-
checkBadInputs(this.root);
384382
}
385383

386384
/**
@@ -397,6 +395,9 @@ export class FormManager {
397395
);
398396
default:
399397
if (element instanceof ValueFormElement) {
398+
if (element instanceof NumberFormElement && element.badInput) {
399+
return 'invalid';
400+
}
400401
return element.value;
401402
}
402403
throw new Error(`Unsupported type ${element.type}`);

jschema/src/lib/components/properties/NumberProperty.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
let validationError = '';
1010
1111
function handleValueChange() {
12-
formElement.notifyChange();
1312
validate();
13+
formElement.notifyChange();
1414
}
1515
1616
function validate() {

tests/v2/jschema.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ test('JSON Schema validation', async ({ page, workflow }) => {
225225
await input.pressSequentially('e');
226226
expect(form.getByText('Should be a number')).toHaveCount(1);
227227
await page.getByRole('button', { name: 'Save changes' }).click();
228-
await page.getByText('Form contains invalid input').waitFor();
228+
await page.getByText('must be integer').waitFor();
229229
await page.getByRole('button', { name: 'Discard changes' }).click();
230230
});
231231

0 commit comments

Comments
 (0)