Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

88
Views
Javascript return value from recursion in async function

Hello hello this is my recursive function which do validation for nested properties. So i am trying to return array with all errors from this function. Clearly i am doing something wrong since there are many loops. Please help me to return array of objects.

export async function validateIt<T>(payload: T, validations: IValidation<T>[], errors = []) {
  return await Promise.all(
    validations.map(async x => {
        return await Promise.all(x.validators?.map(async validator => {
          if (!x.nestedValidations) {
            const results = await validate(validator, payload, x.field)
            errors.push(results)
            return errors
          } else {
            await Promise.all(
              payload[x.field].map(async (nestedPayloadItem) => {
                await validateIt(nestedPayloadItem, x.nestedValidations)
              })
            )
          }
        }))
      }
    )
  )
}

this is how payload looks

    {
        "event": 103,
        "itemCode": "Test",
        "storeProductAvailabilityList": [
            {"startTime": 200, "endTime": 100, "pricingType": "SELLD"},
            {"type": "REG",  "endTime": 100}
        ],
          "storeProductImageList": [
            {"type": "ONSITE", "base64": "3123123123"},
            {"type": "CHART"}
        ]
    }

And here is function call

      const errors = await validateIt<PostStoreProductPayload>(
        payload,
        [{
          field: "eventId",
          validators: [{
            type: 'required'
          }]
        },{
          field: "name",
          validators: [{
            type: 'required'
          }]
        },{
            field: "storeProductImageList",
            validators: [{
              type: 'required'
            }],
            nestedValidations: [{ field: 'base64', validators: [{ type: 'required'}]}, { field: 'type', validators: [{ type: 'enum', values: ['CHART', 'CART']}]} ]
          },
          {
            field: "sku",
            validators: [{
              type: 'required'
            }]
          }]
      )

Getting multidimensional array so my question is this right way to write recursion? Am i doing right return from this part

if (!x.nestedValidations) {
   const results = await validate(validator, payload, x.field)
   errors.push(results)
   return errors
}

I am slightly confused all these "async".

Looking for this result.

[
  { name: 'required' },
  { sku: 'required' }, 

]
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!