Skip to content

Commit 73db143

Browse files
authored
fix: resolve array validation issues with zero values and redundant logic (#137)
* fix(adapter-evm): clean up redundant ternary in array field validation # Conflicts: # packages/adapter-evm/src/mapping/field-generator.ts * fix(ui): allow zero values in ArrayField required validation * fix(adapter-evm): use consistent validation for array elements Apply getDefaultValidationForType to array element validation instead of hardcoded { required: true } for better consistency and to ensure arrays of addresses get proper validation.
1 parent 49d7d6c commit 73db143

File tree

4 files changed

+47
-2
lines changed

4 files changed

+47
-2
lines changed

.changeset/curly-vans-beg.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@openzeppelin/contracts-ui-builder-ui': patch
3+
---
4+
5+
allow zero values in ArrayField required validation

.changeset/whole-bees-press.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@openzeppelin/contracts-ui-builder-adapter-evm': patch
3+
---
4+
5+
clean up redundant ternary in array field validation

packages/adapter-evm/src/mapping/field-generator.ts

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,22 @@ import { isValidEvmAddress } from '../utils';
1313
import { mapEvmParamTypeToFieldType } from './type-mapper';
1414

1515
/**
16-
* Get default validation rules for a parameter type
16+
* Extracts the inner type from an EVM array type.
17+
* @param parameterType - The parameter type (e.g., 'uint32[]', 'address[]')
18+
* @returns The inner type (e.g., 'uint32', 'address') or null if not an array type
19+
*/
20+
function extractArrayElementType(parameterType: string): string | null {
21+
// Handle array types like uint32[], address[], bytes32[]
22+
const arrayMatch = parameterType.match(/^(.+)\[\d*\]$/);
23+
if (arrayMatch) {
24+
return arrayMatch[1];
25+
}
26+
return null;
27+
}
28+
29+
/**
30+
* Get default validation rules for a parameter type.
31+
* Only includes serializable validation rules - no custom functions.
1732
*/
1833
function getDefaultValidationForType(parameterType: string): FieldValidation {
1934
const validation: FieldValidation = { required: true };
@@ -56,6 +71,26 @@ export function generateEvmDefaultField<T extends FieldType = FieldType>(
5671
width: 'full',
5772
};
5873

74+
// For array types, provide element type information
75+
if (fieldType === 'array') {
76+
const elementType = extractArrayElementType(parameter.type);
77+
if (elementType) {
78+
const elementFieldType = mapEvmParamTypeToFieldType(elementType);
79+
80+
// Add array-specific properties
81+
const arrayField = {
82+
...baseField,
83+
elementType: elementFieldType,
84+
elementFieldConfig: {
85+
type: elementFieldType,
86+
validation: getDefaultValidationForType(elementType),
87+
placeholder: `Enter ${elementType}`,
88+
},
89+
};
90+
return arrayField;
91+
}
92+
}
93+
5994
// Preserve components for object and array-object types
6095
if (parameter.components && (fieldType === 'object' || fieldType === 'array-object')) {
6196
const result = {

packages/ui/src/components/fields/ArrayField.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ export function ArrayField<TFieldValues extends FieldValues = FieldValues>({
161161
// should also be callable here, in addition to these built-in length checks.
162162
validate: (value) => {
163163
// Validate array constraints
164-
if (validation?.required && (!value || !Array.isArray(value) || value.length === 0)) {
164+
if (validation?.required && (!Array.isArray(value) || value.length === 0)) {
165165
return 'At least one item is required';
166166
}
167167

0 commit comments

Comments
 (0)