0

This is the code, the object passed has been converted to JSON from a csv sample file using csvtojson. Its failing for string check on the first line. Any clue as to why its saying it received "undefined" when clearly its a string?

`import { z } from 'zod'

const EmployeeSchema = z.object({

    [`Employee Id`]: z.string(),

    [`Full Name`]: z.string(),

    [`Job Title`]: z.string(),

    Department: z.string(),

    [`Business Unit`]: z.string(),

    Gender: z.enum(['Male', 'Female']),

    Ethnicity: z.string(),

    Age: z.string(),

    [`Hire Date`]: z.string(),

    [`Annual Salary`]: z.string(),

    [`Bonus %`]: z.string(),

    Country: z.string(),

    City: z.string(),

    [`Exit Date`]: z.optional(z.string())

})

const ValidationInputSchema = z.array(EmployeeSchema)

const validation = (employeeData: any) => {

    console.log('employeeData', employeeData)

    // const result = employeeData

    try {

        const result = ValidationInputSchema.parse(employeeData)

        console.dir('Result', result)

    } catch (err) {

        // check for ZodError

        if (err instanceof z.ZodError) {

            // loop over suberrors

            for (const suberr of err.errors) {

            console.dir(suberr)

            }

        }

    }

    // const result = ValidationInputSchema.parse(employeeData)

    console.log('# validation done')

    // return result

    return 'done'

}

export {

    validation

}`

This is what it looks like on console:

employeeData [

  {

    'Employee ID': 'E02002',

    'Full Name': 'Kai Le',

    'Job Title': 'Controls Engineer',

    Department: 'Engineering',

    'Business Unit': 'Manufacturing',

    Gender: 'Male',

    Ethnicity: 'Asian',

    Age: '47',

    'Hire Date': '2/5/2022',

    'Annual Salary': '$92,368',

    'Bonus %': '0%',

    Country: 'United States',

    City: 'Columbus',

    'Exit Date': ''

  },

  {

    'Employee ID': 'E02003',

    'Full Name': 'Robert Patel',

    'Job Title': 'Analyst',

    Department: 'Sales',

    'Business Unit': 'Corporate',

    Gender: 'Male',

    Ethnicity: 'Asian',

    Age: '58',

    'Hire Date': '10/23/2013',

    'Annual Salary': '$45,703',

    'Bonus %': '0%',

    Country: 'United States',

    City: 'Chicago',

    'Exit Date': ''

  },

  {

    'Employee ID': 'E02004',

    'Full Name': 'Cameron Lo',

    'Job Title': 'Network Administrator',

    Department: 'IT',

    'Business Unit': 'Research & Development',

    Gender: 'Male',

    Ethnicity: 'Asian',

    Age: '34',

    'Hire Date': '3/24/2019',

    'Annual Salary': '$83,576',

    'Bonus %': '0%',

    Country: 'China',

    City: 'Shanghai',

    'Exit Date': ''

  },

  {

    'Employee ID': 'E02005',

    'Full Name': 'Harper Castillo',

    'Job Title': 'IT Systems Architect',

    Department: 'IT',

    'Business Unit': 'Corporate',

    Gender: 'Female',

    Ethnicity: 'Latino',

    Age: '39',

    'Hire Date': '4/7/2018',

    'Annual Salary': '$98,062',

    'Bonus %': '0%',

    Country: 'United States',

    City: 'Seattle',

    'Exit Date': ''

  }

]

{

  code: 'invalid_type',

  expected: 'string',

  received: 'undefined',

  path: [ 0, 'Employee Id' ],

  message: 'Required'

}

{

  code: 'invalid_type',

  expected: 'string',

  received: 'undefined',

  path: [ 1, 'Employee Id' ],

  message: 'Required'

}

{

  code: 'invalid_type',

  expected: 'string',

  received: 'undefined',

  path: [ 2, 'Employee Id' ],

  message: 'Required'

}

{

  code: 'invalid_type',

  expected: 'string',

  received: 'undefined',

  path: [ 3, 'Employee Id' ],

  message: 'Required'

}

validation done


I tried to convert a csv to json and then validate the json using zod. But its saying the type is unknown.

0 Answers0