Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

Validation error with valid email address

default discord avatar
ajb_999last year

I'm attempting to implement automated testing using Mocha and I've run into an issue with creating the initial account on my Payload instance.



In my test script, I first delete all indexes and records from all collections in the DB, and then the obvious next step is to crate the initial Account (what I call my Users collection). However, when I attempt to use the REST API to create the initial account I get the following error:


{
   "errors": [
      {
         "name": "ValidationError",
         "data": [
            {
               "field": "email",
               "message": "Please enter a valid email address."
            }
         ],
         "message": "The following field is invalid: email"
      }
   ]
}


But the email address I'm providing is a valid email. Anyone have any idea why I'm getting this validation error?



Here's the relevant CLI output:


...
webpack compiled successfully
[01:35:41] ERROR (payload): ValidationError: The following field is invalid: email
    at beforeChange (/Users/ajb/Server/zazzanova/api/node_modules/payload/src/fields/hooks/beforeChange/index.ts:60:11)
    at create (/Users/ajb/Server/zazzanova/api/node_modules/payload/src/collections/operations/create.ts:195:31)
    at createHandler (/Users/ajb/Server/zazzanova/api/node_modules/payload/src/collections/requestHandlers/create.ts:26:17)


  DB RESET
    ✔ should zork payload-preferences (942ms)
    ✔ should zork payload-migrations (193ms)
    ✔ should zork accounts (219ms)

  INIT ROOT ACCOUNT
{
   "errors": [
      {
         "name": "ValidationError",
         "data": [
            {
               "field": "email",
               "message": "Please enter a valid email address."
            }
         ],
         "message": "The following field is invalid: email"
      }
   ]
}
    1) should create the root account


  3 passing (1s)
  1 failing

  1) INIT ROOT ACCOUNT
       should create the root account:
     AssertionError: expected [ { name: 'ValidationError', …(2) } ] to not exist
      at should2.not.exist (file:///Users/ajb/Server/zazzanova/api/node_modules/chai/chai.js:3110:36)
      at Context.<anonymous> (file:///Users/ajb/Server/zazzanova/api/test/reset.mjs:92:14)
      at process.processTicksAndRejections (node:internal/process/task_queues:95:5)


And here's the test script:


describe('INIT ROOT ACCOUNT', () => {
    it('should create the root account', async () => {
        const request = await fetch('http://localhost:3000/api/accounts', {
            method: 'POST',
            body: JSON.stringify({
                email: 'myemail@mydomain.com',
                password: '12345'
            })
        })

        const data = await request.json()
        log(data)

        should.not.exist(data.errors)
    })
})


Thanks in advance for any help!



Nevermind, I just figured it out 30 seconds after posting this.



The problem was that I wasn't including the

'Content-Type': 'application/json'

header in my

POST

request to create the "root" user.



Why this throws a validation error is beyond me.

    Star on GitHub

    Star

    Chat on Discord

    Discord

    online

    Can't find what you're looking for?

    Get dedicated engineering support directly from the Payload team.