Skip to content

Commit cd4516a

Browse files
committed
fix: validation issues resolved
1 parent 523f24d commit cd4516a

File tree

8 files changed

+66
-28
lines changed

8 files changed

+66
-28
lines changed

src/demo/examples/simple/ui-schema.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
"ui:page": {
33
"ui:layout": "tabs",
44
"props": {
5-
"tabIndex": 1,
65
"ui:schemaErrors": true
76
},
87
"style": {

src/helpers/state-machine/form/actions/form.actions.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import { StateMachineInstance } from '../../types/form-state-machine.type';
1919
* @returns
2020
*/
2121
const useFormActions = ({
22-
isStepperUI,
22+
isPartialUI,
2323
}) => {
2424
const {
2525
FORM_ACTIONS: actions,
@@ -40,7 +40,7 @@ const useFormActions = ({
4040
const {
4141
uiSchema: currentUISchema,
4242
} = state.context;
43-
const formValidCondition = isStepperUI(currentUISchema) ? state.value.formUI === FORM_STATE_ERROR_EVENTS.INVALID
43+
const formValidCondition = isPartialUI(currentUISchema) ? state.value.formUI === FORM_STATE_ERROR_EVENTS.INVALID
4444
: Object.values(state.value).includes(FORM_STATE_ERROR_EVENTS.INVALID);
4545

4646
const PROPAGATE_ON_CHANGE_CONDITION = {
@@ -97,6 +97,7 @@ const useFormActions = ({
9797
formData: currentData,
9898
uiData: currentUIData,
9999
uiSchema: currentUISchema,
100+
formSchemaXHR,
100101
validations,
101102
activeStep,
102103
hasError,
@@ -116,7 +117,8 @@ const useFormActions = ({
116117
stateMachineService,
117118
state,
118119
buttonDisabled,
119-
isStepperUI,
120+
isPartialUI,
121+
formSchemaXHR,
120122
});
121123

122124
if (!schemaErrors && !isError && hasError) {

src/helpers/state-machine/form/actions/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ interface ExecuteFormActions {
1717
}
1818

1919
const useActions = ({
20-
isStepperUI,
20+
isPartialUI,
2121
}) => {
2222
const [buttonDisabled, setButtonDisabled] = React.useState(false);
2323

@@ -29,7 +29,7 @@ const useActions = ({
2929
const {
3030
getValidActionToExecute,
3131
executeFormActions,
32-
} = useFormActions({ isStepperUI });
32+
} = useFormActions({ isPartialUI });
3333

3434
const executeAction = {
3535
[FORM_STATE_CONFIG.FORM_ACTIONS.DISABLE_FORM_SUBMIT]: () => setButtonDisabled(true),

src/helpers/state-machine/form/form-state.guards.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,14 @@ const GUARDS = {
4040
// console.log(er);
4141
},
4242
},
43+
formSchemaXHR: context.formSchemaXHR,
4344
state: { context },
4445
buttonDisabled: false,
45-
isStepperUI: (currentUISchema) => get(
46+
isPartialUI: (currentUISchema) => get(
4647
currentUISchema, 'ui:page.ui:layout',
47-
) === 'steps',
48+
) === 'steps' || get(
49+
currentUISchema, 'ui:page.ui:layout',
50+
) === 'tabs',
4851
});
4952
isSchemaValid = Array.isArray(schemaErrors) ? schemaErrors.length === 0 && !isError : !schemaErrors && !isError;
5053
}

src/helpers/state-machine/form/hooks/form-state-machine.hooks.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,16 @@ const useFormStateMachine = ({
7777
} : {};
7878
const givenFormInfo = !formStateMachine ? originalFormInfo : stateFormInfo;
7979
const [loadingState] = React.useState(null);
80-
const isStepperUI = (uiSchema) => get(
80+
const isPartialUI = (uiSchema) => get(
8181
uiSchema, 'ui:page.ui:layout',
82-
) === 'steps';
82+
) === 'steps' || get(
83+
uiSchema, 'ui:page.ui:layout',
84+
) === 'tabs';
8385
const {
8486
executeFormActionsByState,
8587
buttonDisabled,
8688
} = useFormActions({
87-
isStepperUI,
89+
isPartialUI,
8890
});
8991

9092
const startMachine = (givenInfo: FormContext) => {
@@ -98,12 +100,13 @@ const useFormStateMachine = ({
98100
currentSchema: schema,
99101
currentUISchema: uiSchema,
100102
currentData: formData,
103+
formSchemaXHR: {},
101104
validations,
102105
activeStep,
103106
stateMachineService,
104107
state: stateMachineService?.state,
105108
buttonDisabled,
106-
isStepperUI,
109+
isPartialUI,
107110
});
108111
formStateMachine = createStateMachine({
109112
uiSchema,
@@ -165,7 +168,7 @@ const useFormStateMachine = ({
165168
stateMachineService,
166169
buttonDisabled: givenFormInfo?.hasError || false,
167170
loadingState,
168-
isStepperUI,
171+
isPartialUI,
169172
};
170173
};
171174

src/helpers/state-machine/form/mutations/form-state.mutations.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ const FormMutations = {
137137
),
138138
}),
139139
updateTabIndex: assign({
140+
activeStep: (context: FormContext, event: EventObject & { tabIndex: number }) => event.tabIndex,
140141
uiSchema: (context: FormContext, event: EventObject & { tabIndex: number }) => ({
141142
...context.uiSchema,
142143
'ui:page': {

src/helpers/state-machine/helpers/is-form-schema-state-valid.ts

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,16 @@ const isFormSchemaStateValid = ({
1818
validation,
1919
buttonDisabled,
2020
stateMachineService,
21+
formSchemaXHR = {},
22+
isStepper,
2123
}) => {
2224
if (get(uiSchema, 'ui:page.props.ui:schemaErrors')
2325
|| !has(uiSchema, 'ui:page.props.ui:schemaErrors')) {
2426
try {
25-
const transformedSchema = transformSchema(schema);
27+
const transformedSchema = transformSchema({
28+
...schema,
29+
...formSchemaXHR,
30+
});
2631
const ajv = new Ajv();
2732
const validate = ajv.compile(transformedSchema);
2833
// eslint-disable-next-line @typescript-eslint/no-floating-promises
@@ -36,10 +41,12 @@ const isFormSchemaStateValid = ({
3641

3742
const externalValidations = isFormInValid(validation);
3843
if (externalValidations && !buttonDisabled) {
39-
stateMachineService.send(FORM_STATE_CONFIG.FORM_STATE_ERROR_EVENTS.ERROR, {
40-
hasError: externalValidations,
41-
validation,
42-
});
44+
if (stateMachineService && typeof stateMachineService.send === 'function') {
45+
stateMachineService.send(FORM_STATE_CONFIG.FORM_STATE_ERROR_EVENTS.ERROR, {
46+
hasError: externalValidations,
47+
validation,
48+
});
49+
}
4350
return {
4451
schemaErrors: externalValidations,
4552
transformedSchema,
@@ -48,10 +55,12 @@ const isFormSchemaStateValid = ({
4855

4956
if (!externalValidations && validate.errors && !buttonDisabled) {
5057
validate.errors.forEach((err) => {
51-
stateMachineService.send(FORM_STATE_CONFIG.FORM_STATE_ERROR_EVENTS.ERROR, {
52-
hasError: err,
53-
validation,
54-
});
58+
if (stateMachineService && typeof stateMachineService.send === 'function') {
59+
stateMachineService.send(FORM_STATE_CONFIG.FORM_STATE_ERROR_EVENTS.ERROR, {
60+
hasError: err,
61+
validation,
62+
});
63+
}
5564
});
5665
return {
5766
schemaErrors: validate.errors,
@@ -65,7 +74,20 @@ const isFormSchemaStateValid = ({
6574
};
6675
}
6776
catch (err) {
68-
// console.log('err', err);
77+
if (!isStepper) {
78+
const transformedSchema = transformSchema({
79+
...schema,
80+
...formSchemaXHR,
81+
});
82+
return {
83+
schemaErrors: [{
84+
"rule": "schemaError",
85+
"title": "Invalid Form Schema",
86+
"message": String(err)
87+
}],
88+
transformedSchema,
89+
};
90+
}
6991
}
7092
}
7193

src/helpers/validation/index.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export { default } from './get-validation-result';
1313
* @returns { Schema, data }
1414
*/
1515
const getSchemaAndFormData = ({
16-
isStepperUI,
16+
isPartialUI,
1717
currentSchema: givenSchema,
1818
currentData: givenData,
1919
currentUISchema,
@@ -28,12 +28,12 @@ const getSchemaAndFormData = ({
2828
currentSchema.properties[stepName],
2929
currentData[stepName],
3030
);
31-
const schema = isStepperUI(currentUISchema)
31+
const schema = isPartialUI(currentUISchema)
3232
? {
3333
...translatedSchema,
3434
}
3535
: currentSchema;
36-
const data = isStepperUI(currentUISchema) ? currentData[stepName] : currentData;
36+
const data = isPartialUI(currentUISchema) ? currentData[stepName] : currentData;
3737
return {
3838
schema,
3939
data,
@@ -59,7 +59,8 @@ export const hasSchemaErrors = ({
5959
stateMachineService,
6060
state,
6161
buttonDisabled,
62-
isStepperUI,
62+
isPartialUI,
63+
formSchemaXHR,
6364
}) => {
6465
const validation = getValidationResult(
6566
currentSchema,
@@ -77,7 +78,7 @@ export const hasSchemaErrors = ({
7778
currentSchema,
7879
currentUISchema,
7980
activeStep,
80-
isStepperUI,
81+
isPartialUI,
8182
});
8283

8384
const { schemaErrors, transformedSchema } = isFormSchemaStateValid({
@@ -88,6 +89,13 @@ export const hasSchemaErrors = ({
8889
data,
8990
onError: state?.context?.effects?.onError,
9091
buttonDisabled,
92+
formSchemaXHR,
93+
isStepper: (
94+
currentUISchema
95+
&& currentUISchema['ui:page']
96+
&& currentUISchema['ui:page']['ui:layout']
97+
&& currentUISchema['ui:page']['ui:layout'] === 'steps'
98+
)
9199
});
92100

93101
return {

0 commit comments

Comments
 (0)