Skip to content

Commit

Permalink
feat: better form array validation (#2822)
Browse files Browse the repository at this point in the history
  • Loading branch information
waterplea authored Jan 28, 2025
1 parent bb8109f commit b2b9864
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
1 change: 1 addition & 0 deletions web/projects/ui/src/app/services/api/api.fixures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1719,6 +1719,7 @@ export module Mock {
ISB.List.text(
{
name: 'RPC Auth',
minLength: 3,
description:
'api keys that are authorized to access your Bitcoin node.',
},
Expand Down
20 changes: 13 additions & 7 deletions web/projects/ui/src/app/services/form.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ export class FormService {

getListItem(spec: IST.ValueSpecList, entry?: any) {
if (IST.isValueSpecListOf(spec, 'text')) {
return this.formBuilder.control(entry, stringValidators(spec.spec))
return this.formBuilder.control(entry, [
...stringValidators(spec.spec),
Validators.required,
])
} else if (IST.isValueSpecListOf(spec, 'object')) {
return this.getFormGroup(spec.spec.spec, [], entry)
}
Expand Down Expand Up @@ -116,12 +119,15 @@ export class FormService {
case 'object':
return this.getFormGroup(spec.spec, [], currentValue)
case 'list':
const mapped = (
Array.isArray(currentValue) ? currentValue : (spec.default as any[])
).map(entry => {
return this.getListItem(spec, entry)
})
return this.formBuilder.array(mapped, listValidators(spec))
const array = Array.isArray(currentValue) ? currentValue : spec.default
const length = Math.max(array.length, spec.minLength || 0)

return this.formBuilder.array(
Array.from({ length }).map((_, index) =>
this.getListItem(spec, array[index]),
),
listValidators(spec),
)
case 'file':
return this.formBuilder.control(
currentValue || null,
Expand Down

0 comments on commit b2b9864

Please sign in to comment.