fix(ui): add validation to field value reducers (#5256)

## What type of PR is this? (check all applicable)

- [ ] Refactor
- [ ] Feature
- [x] Bug Fix
- [ ] Optimization
- [ ] Documentation Update
- [ ] Community Node Submission

## Description

Insurance against invalid inputs. Closes #5250
This commit is contained in:
blessedcoolant 2023-12-09 11:42:32 +05:30 committed by GitHub
commit 40d4c7c8e1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -20,6 +20,22 @@ import {
StringFieldValue, StringFieldValue,
T2IAdapterModelFieldValue, T2IAdapterModelFieldValue,
VAEModelFieldValue, VAEModelFieldValue,
zBoardFieldValue,
zBooleanFieldValue,
zColorFieldValue,
zControlNetModelFieldValue,
zEnumFieldValue,
zFloatFieldValue,
zImageFieldValue,
zIntegerFieldValue,
zIPAdapterModelFieldValue,
zLoRAModelFieldValue,
zMainModelFieldValue,
zSchedulerFieldValue,
zSDXLRefinerModelFieldValue,
zStringFieldValue,
zT2IAdapterModelFieldValue,
zVAEModelFieldValue,
} from 'features/nodes/types/field'; } from 'features/nodes/types/field';
import { import {
AnyNode, AnyNode,
@ -58,6 +74,7 @@ import {
appSocketQueueItemStatusChanged, appSocketQueueItemStatusChanged,
} from 'services/events/actions'; } from 'services/events/actions';
import { v4 as uuidv4 } from 'uuid'; import { v4 as uuidv4 } from 'uuid';
import { z } from 'zod';
import { NodesState } from './types'; import { NodesState } from './types';
import { findConnectionToValidHandle } from './util/findConnectionToValidHandle'; import { findConnectionToValidHandle } from './util/findConnectionToValidHandle';
import { findUnoccupiedPosition } from './util/findUnoccupiedPosition'; import { findUnoccupiedPosition } from './util/findUnoccupiedPosition';
@ -106,7 +123,8 @@ type FieldValueAction<T extends FieldValue> = PayloadAction<{
const fieldValueReducer = <T extends FieldValue>( const fieldValueReducer = <T extends FieldValue>(
state: NodesState, state: NodesState,
action: FieldValueAction<T> action: FieldValueAction<T>,
schema: z.ZodTypeAny
) => { ) => {
const { nodeId, fieldName, value } = action.payload; const { nodeId, fieldName, value } = action.payload;
const nodeIndex = state.nodes.findIndex((n) => n.id === nodeId); const nodeIndex = state.nodes.findIndex((n) => n.id === nodeId);
@ -115,12 +133,10 @@ const fieldValueReducer = <T extends FieldValue>(
return; return;
} }
const input = node.data?.inputs[fieldName]; const input = node.data?.inputs[fieldName];
if (!input) { if (!input || nodeIndex < 0 || !schema.safeParse(value).success) {
return; return;
} }
if (nodeIndex > -1) { input.value = value;
input.value = value;
}
}; };
const nodesSlice = createSlice({ const nodesSlice = createSlice({
@ -527,91 +543,91 @@ const nodesSlice = createSlice({
state, state,
action: FieldValueAction<StringFieldValue> action: FieldValueAction<StringFieldValue>
) => { ) => {
fieldValueReducer(state, action); fieldValueReducer(state, action, zStringFieldValue);
}, },
fieldNumberValueChanged: ( fieldNumberValueChanged: (
state, state,
action: FieldValueAction<IntegerFieldValue | FloatFieldValue> action: FieldValueAction<IntegerFieldValue | FloatFieldValue>
) => { ) => {
fieldValueReducer(state, action); fieldValueReducer(state, action, zIntegerFieldValue.or(zFloatFieldValue));
}, },
fieldBooleanValueChanged: ( fieldBooleanValueChanged: (
state, state,
action: FieldValueAction<BooleanFieldValue> action: FieldValueAction<BooleanFieldValue>
) => { ) => {
fieldValueReducer(state, action); fieldValueReducer(state, action, zBooleanFieldValue);
}, },
fieldBoardValueChanged: ( fieldBoardValueChanged: (
state, state,
action: FieldValueAction<BoardFieldValue> action: FieldValueAction<BoardFieldValue>
) => { ) => {
fieldValueReducer(state, action); fieldValueReducer(state, action, zBoardFieldValue);
}, },
fieldImageValueChanged: ( fieldImageValueChanged: (
state, state,
action: FieldValueAction<ImageFieldValue> action: FieldValueAction<ImageFieldValue>
) => { ) => {
fieldValueReducer(state, action); fieldValueReducer(state, action, zImageFieldValue);
}, },
fieldColorValueChanged: ( fieldColorValueChanged: (
state, state,
action: FieldValueAction<ColorFieldValue> action: FieldValueAction<ColorFieldValue>
) => { ) => {
fieldValueReducer(state, action); fieldValueReducer(state, action, zColorFieldValue);
}, },
fieldMainModelValueChanged: ( fieldMainModelValueChanged: (
state, state,
action: FieldValueAction<MainModelFieldValue> action: FieldValueAction<MainModelFieldValue>
) => { ) => {
fieldValueReducer(state, action); fieldValueReducer(state, action, zMainModelFieldValue);
}, },
fieldRefinerModelValueChanged: ( fieldRefinerModelValueChanged: (
state, state,
action: FieldValueAction<SDXLRefinerModelFieldValue> action: FieldValueAction<SDXLRefinerModelFieldValue>
) => { ) => {
fieldValueReducer(state, action); fieldValueReducer(state, action, zSDXLRefinerModelFieldValue);
}, },
fieldVaeModelValueChanged: ( fieldVaeModelValueChanged: (
state, state,
action: FieldValueAction<VAEModelFieldValue> action: FieldValueAction<VAEModelFieldValue>
) => { ) => {
fieldValueReducer(state, action); fieldValueReducer(state, action, zVAEModelFieldValue);
}, },
fieldLoRAModelValueChanged: ( fieldLoRAModelValueChanged: (
state, state,
action: FieldValueAction<LoRAModelFieldValue> action: FieldValueAction<LoRAModelFieldValue>
) => { ) => {
fieldValueReducer(state, action); fieldValueReducer(state, action, zLoRAModelFieldValue);
}, },
fieldControlNetModelValueChanged: ( fieldControlNetModelValueChanged: (
state, state,
action: FieldValueAction<ControlNetModelFieldValue> action: FieldValueAction<ControlNetModelFieldValue>
) => { ) => {
fieldValueReducer(state, action); fieldValueReducer(state, action, zControlNetModelFieldValue);
}, },
fieldIPAdapterModelValueChanged: ( fieldIPAdapterModelValueChanged: (
state, state,
action: FieldValueAction<IPAdapterModelFieldValue> action: FieldValueAction<IPAdapterModelFieldValue>
) => { ) => {
fieldValueReducer(state, action); fieldValueReducer(state, action, zIPAdapterModelFieldValue);
}, },
fieldT2IAdapterModelValueChanged: ( fieldT2IAdapterModelValueChanged: (
state, state,
action: FieldValueAction<T2IAdapterModelFieldValue> action: FieldValueAction<T2IAdapterModelFieldValue>
) => { ) => {
fieldValueReducer(state, action); fieldValueReducer(state, action, zT2IAdapterModelFieldValue);
}, },
fieldEnumModelValueChanged: ( fieldEnumModelValueChanged: (
state, state,
action: FieldValueAction<EnumFieldValue> action: FieldValueAction<EnumFieldValue>
) => { ) => {
fieldValueReducer(state, action); fieldValueReducer(state, action, zEnumFieldValue);
}, },
fieldSchedulerValueChanged: ( fieldSchedulerValueChanged: (
state, state,
action: FieldValueAction<SchedulerFieldValue> action: FieldValueAction<SchedulerFieldValue>
) => { ) => {
fieldValueReducer(state, action); fieldValueReducer(state, action, zSchedulerFieldValue);
}, },
notesNodeValueChanged: ( notesNodeValueChanged: (
state, state,