I create a payload app using
npx create-payload-app
and I chose to use blank template
I have this. ValidationError: The following field is invalid: email
src/seed/index.ts
import { Payload } from 'payload';
import { customerForm } from './customerForm';
export const seed = async (payload: Payload) => {
const customerFormJSON = JSON.parse(JSON.stringify(customerForm));
const { id: customerFormID } = await payload.create({
collection: 'customer',
data: customerFormJSON,
});
};
src/seed/customerForm.ts
export const customerForm = {
id: '63c0651b132c8e2783f8dcae',
updatedAt: '2023-01-12T21:25:41.113Z',
createdAt: '2022-12-28T20:48:53.181Z',
title: 'Customer Form',
fields: [
{
name: 'firstName',
label: 'First name',
width: 50,
required: true,
id: '63adaaba5236fe69ca8973f8',
blockName: 'first-name',
blockType: 'text',
},
{
name: 'lastName',
label: 'Last name',
width: 50,
required: true,
id: '63bf4b1fd69cef4f34272f9a',
blockName: 'last-name',
blockType: 'text',
},
{
name: 'email',
label: 'Email',
width: 100,
required: true,
id: '63bf4b1fd69cef4f34272f9b',
blockName: 'email',
blockType: 'text',
},
],
redirect: {},
};
src/collections/Customers.ts
import { CollectionConfig, Block } from 'payload/types';
export const Customers: CollectionConfig = {
slug: 'customer',
// auth: true,
fields: [
{
name: 'firstName',
type: 'text',
// required: true,
},
{
name: 'lastName',
type: 'text',
// required: true,
},
{
name: 'email',
type: 'text',
// required: true,
},
]
}
src/server.ts
import express from 'express';
import payload from 'payload';
import { seed } from './seed'
require('dotenv').config();
const app = express();
// Redirect root to Admin panel
app.get('/', (_, res) => {
res.redirect('/admin');
});
const start = async () => {
// Initialize Payload
await payload.init({
secret: process.env.PAYLOAD_SECRET,
mongoURL: process.env.MONGODB_URI,
express: app,
email: {
fromName: 'Admin',
fromAddress: '[email protected]',
logMockCredentials: true,
},
onInit: async () => {
payload.logger.info(`Payload Admin URL: ${payload.getAdminURL()}`)
},
})
// Add your own express routes here
if (process.env.PAYLOAD_SEED === 'true') {
payload.logger.info('---- SEEDING DATABASE ----');
await seed(payload);
}
app.listen(3000);
}
start();
When I uncomment (required: true) in customers.ts I get this error: ValidationError: followingFieldsInvalid firstName, lastName
when I comment (required: true) in customers.ts ValidationError: The following field is invalid: email
NB: I only want to use payload.create for creating data.
Any help is appreciated
I figured the issue. The issue was that I deleted/commented the unique constraint on the on the fields email, firstName or lastName but forgot to update the database. Even though I removed the unique constraint from your Payload CMS collection configuration (for email, firstName, lastName, or name) , MongoDB might still enforce it at the database level if it wasn't properly updated after my changes. I manually inspected my MongoDB collection's indexes to confirm whether the unique constraint on the 'name' field still exists.
like this: