Skip to content

Commit 27ee978

Browse files
committed
dynamically return errors based in model state
1 parent 20033f5 commit 27ee978

File tree

1 file changed

+27
-19
lines changed

1 file changed

+27
-19
lines changed

src/demo/App.tsx

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -416,29 +416,37 @@ export default function App() {
416416
// Simulate API call that returns validation errors
417417
await new Promise(resolve => setTimeout(resolve, 500))
418418

419-
const errors: ResponseStatus = {
419+
const errors = []
420+
if (!request.name) {
421+
errors.push({
422+
fieldName: 'name',
423+
errorCode: 'NotEmpty',
424+
message: 'Name is required and cannot be empty'
425+
})
426+
}
427+
if (!request.email) {
428+
errors.push({
429+
fieldName: 'email',
430+
errorCode: 'InvalidEmail',
431+
message: 'Please enter a valid email address'
432+
})
433+
}
434+
if (!request.age || request.age < 18 || request.age > 120) {
435+
errors.push({
436+
fieldName: 'age',
437+
errorCode: 'Range',
438+
message: 'Age must be between 18 and 120'
439+
})
440+
}
441+
const status: ResponseStatus = {
420442
errorCode: 'ValidationError',
421443
message: 'Validation failed',
422-
errors: [
423-
{
424-
fieldName: 'name',
425-
errorCode: 'NotEmpty',
426-
message: 'Name is required and cannot be empty'
427-
},
428-
{
429-
fieldName: 'email',
430-
errorCode: 'InvalidEmail',
431-
message: 'Please enter a valid email address'
432-
},
433-
{
434-
fieldName: 'age',
435-
errorCode: 'Range',
436-
message: 'Age must be between 18 and 120'
437-
}
438-
]
444+
errors
439445
}
440446

441-
return new ApiResult({ error: errors })
447+
return errors.length
448+
? new ApiResult({ error: status })
449+
: new ApiResult({ response: new EmptyResponse() })
442450
}}
443451
/>
444452
</div>

0 commit comments

Comments
 (0)