Compare commits

...

5 Commits

Author SHA1 Message Date
Jacob Fletcher
96ddc79788 fix: field level validation errors were incorrectly throwing uniqueness errors 2025-01-16 09:23:45 -05:00
Jacob Fletcher
e45ce23cd7 resets community test suite 2025-01-14 16:57:37 -05:00
Jacob Fletcher
f17f36b62a removes log 2025-01-14 16:56:15 -05:00
Jacob Fletcher
802c4006b1 more validation skips 2025-01-14 16:53:51 -05:00
Jacob Fletcher
dc1b9bd8d6 perf: skips field validations until the form is submitted 2025-01-14 16:22:38 -05:00
18 changed files with 149 additions and 108 deletions

View File

@@ -10,7 +10,7 @@ export const handleError = ({
req,
}: {
collection?: string
error: unknown
error: any
global?: string
req?: Partial<PayloadRequest>
}) => {
@@ -18,8 +18,6 @@ export const handleError = ({
throw error
}
const message = req?.t ? req.t('error:valueMustBeUnique') : 'Value must be unique'
// Handle uniqueness error from MongoDB
if ('code' in error && error.code === 11000 && 'keyValue' in error && error.keyValue) {
throw new ValidationError(
@@ -27,7 +25,7 @@ export const handleError = ({
collection,
errors: [
{
message,
message: req?.t ? req.t('error:valueMustBeUnique') : 'Value must be unique',
path: Object.keys(error.keyValue)[0],
},
],
@@ -37,5 +35,5 @@ export const handleError = ({
)
}
throw new APIError(message, httpStatus.BAD_REQUEST)
throw new APIError(error.message, httpStatus.BAD_REQUEST)
}

View File

@@ -88,6 +88,7 @@ export const Account: React.FC<AdminViewProps> = async ({
renderAllFields: true,
req,
schemaPath: collectionConfig.slug,
skipValidation: true,
})
// Fetch document lock state

View File

@@ -48,7 +48,7 @@ export const CreateFirstUserClient: React.FC<{
const collectionConfig = getEntityConfig({ collectionSlug: userSlug }) as ClientCollectionConfig
const onChange: FormProps['onChange'][0] = React.useCallback(
async ({ formState: prevFormState }) => {
async ({ formState: prevFormState, submitted }) => {
const controller = handleAbortRef(abortOnChangeRef)
const response = await getFormState({
@@ -59,6 +59,7 @@ export const CreateFirstUserClient: React.FC<{
operation: 'create',
schemaPath: userSlug,
signal: controller.signal,
skipValidation: !submitted,
})
abortOnChangeRef.current = null

View File

@@ -63,6 +63,7 @@ export const CreateFirstUserView: React.FC<AdminViewProps> = async ({ initPageRe
renderAllFields: true,
req,
schemaPath: collectionConfig.slug,
skipValidation: true,
})
return (

View File

@@ -155,6 +155,7 @@ export const renderDocument = async ({
renderAllFields: true,
req,
schemaPath: collectionSlug || globalSlug,
skipValidation: true,
}),
])

View File

@@ -225,6 +225,7 @@ const PreviewView: React.FC<Props> = ({
returnLockStatus: false,
schemaPath: entitySlug,
signal: controller.signal,
skipValidation: true,
})
// Unlock the document after save
@@ -267,7 +268,7 @@ const PreviewView: React.FC<Props> = ({
)
const onChange: FormProps['onChange'][0] = useCallback(
async ({ formState: prevFormState }) => {
async ({ formState: prevFormState, submitted }) => {
const controller = handleAbortRef(abortOnChangeRef)
const currentTime = Date.now()
@@ -292,6 +293,7 @@ const PreviewView: React.FC<Props> = ({
returnLockStatus: isLockingEnabled ? true : false,
schemaPath,
signal: controller.signal,
skipValidation: !submitted,
updateLastEdited,
})

View File

@@ -84,6 +84,7 @@ export type BuildFormStateArgs = {
req: PayloadRequest
returnLockStatus?: boolean
schemaPath: string
skipValidation?: boolean
updateLastEdited?: boolean
} & (
| {

View File

@@ -110,7 +110,7 @@ export function EditForm({ submitted }: EditFormProps) {
)
const onChange: NonNullable<FormProps['onChange']>[0] = useCallback(
async ({ formState: prevFormState }) => {
async ({ formState: prevFormState, submitted }) => {
const controller = handleAbortRef(abortOnChangeRef)
const docPreferences = await getDocPreferences()
@@ -123,6 +123,7 @@ export function EditForm({ submitted }: EditFormProps) {
operation: 'create',
schemaPath,
signal: controller.signal,
skipValidation: !submitted,
})
abortOnChangeRef.current = null

View File

@@ -211,6 +211,7 @@ export function FormsManagerProvider({ children }: FormsManagerProps) {
operation: 'create',
renderAllFields: true,
schemaPath: collectionSlug,
skipValidation: true,
})
initialStateRef.current = formStateWithoutFiles
setHasInitializedState(true)

View File

@@ -177,6 +177,7 @@ export const EditMany: React.FC<EditManyProps> = (props) => {
operation: 'update',
schemaPath: slug,
signal: controller.signal,
skipValidation: true,
})
setInitialState(result)
@@ -192,7 +193,7 @@ export const EditMany: React.FC<EditManyProps> = (props) => {
}, [apiRoute, hasInitializedState, serverURL, slug, getFormState, user, collectionPermissions])
const onChange: FormProps['onChange'][0] = useCallback(
async ({ formState: prevFormState }) => {
async ({ formState: prevFormState, submitted }) => {
const controller = handleAbortRef(abortFormStateRef)
const { state } = await getFormState({
@@ -203,6 +204,7 @@ export const EditMany: React.FC<EditManyProps> = (props) => {
operation: 'update',
schemaPath: slug,
signal: controller.signal,
skipValidation: !submitted,
})
abortFormStateRef.current = null

View File

@@ -504,6 +504,7 @@ export const Form: React.FC<FormProps> = (props) => {
renderAllFields: true,
schemaPath: collectionSlug ? collectionSlug : globalSlug,
signal: controller.signal,
skipValidation: true,
})
contextRef.current = { ...initContextState } as FormContextType
@@ -664,6 +665,7 @@ export const Form: React.FC<FormProps> = (props) => {
// Edit view default onChange is in packages/ui/src/views/Edit/index.tsx. This onChange usually sends a form state request
revalidatedFormState = await onChangeFn({
formState: deepCopyObjectSimpleWithoutReactComponents(contextRef.current.fields),
submitted,
})
}
@@ -698,7 +700,7 @@ export const Form: React.FC<FormProps> = (props) => {
`fields` updates before `modified`, because setModified is in a setTimeout.
So on the first change, modified is false, so we don't trigger the effect even though we should.
**/
[contextRef.current.fields, modified],
[contextRef.current.fields, modified, submitted],
[dispatchFields, onChange],
{
delay: 250,

View File

@@ -39,7 +39,7 @@ export type FormProps = {
initialState?: FormState
isInitializing?: boolean
log?: boolean
onChange?: ((args: { formState: FormState }) => Promise<FormState>)[]
onChange?: ((args: { formState: FormState; submitted?: boolean }) => Promise<FormState>)[]
onSubmit?: (fields: FormState, data: Data) => void
onSuccess?: (json: unknown) => Promise<FormState | void> | void
redirect?: string

View File

@@ -47,34 +47,33 @@ type Args = {
renderAllFields: boolean
renderFieldFn?: RenderFieldMethod
req: PayloadRequest
schemaPath: string
skipValidation?: boolean
}
export const fieldSchemasToFormState = async (args: Args): Promise<FormState> => {
if (!args.clientFieldSchemaMap && args.renderFieldFn) {
export const fieldSchemasToFormState = async ({
id,
clientFieldSchemaMap,
collectionSlug,
data = {},
fields,
fieldSchemaMap,
operation,
permissions,
preferences,
previousFormState,
renderAllFields,
renderFieldFn,
req,
schemaPath,
skipValidation,
}: Args): Promise<FormState> => {
if (!clientFieldSchemaMap && renderFieldFn) {
console.warn(
'clientFieldSchemaMap is not passed to fieldSchemasToFormState - this will reduce performance',
)
}
const {
id,
clientFieldSchemaMap,
collectionSlug,
data = {},
fields,
fieldSchemaMap,
operation,
permissions,
preferences,
previousFormState,
renderAllFields,
renderFieldFn,
req,
schemaPath,
} = args
if (fields && fields.length) {
const state: FormStateWithoutComponents = {}
@@ -110,6 +109,7 @@ export const fieldSchemasToFormState = async (args: Args): Promise<FormState> =>
renderAllFields,
renderFieldFn,
req,
skipValidation,
state,
})

View File

@@ -114,6 +114,7 @@ export const buildFormState = async (
},
returnLockStatus,
schemaPath = collectionSlug || globalSlug,
skipValidation,
updateLastEdited,
} = args
@@ -194,6 +195,7 @@ export const buildFormState = async (
renderFieldFn: renderField,
req,
schemaPath,
skipValidation,
})
// Maintain form state of auth / upload fields

View File

@@ -286,6 +286,7 @@ export const DefaultEditView: React.FC<ClientSideEditViewProps> = ({
returnLockStatus: false,
schemaPath: schemaPathSegments.join('.'),
signal: controller.signal,
skipValidation: true,
})
// Unlock the document after save
@@ -329,7 +330,7 @@ export const DefaultEditView: React.FC<ClientSideEditViewProps> = ({
)
const onChange: FormProps['onChange'][0] = useCallback(
async ({ formState: prevFormState }) => {
async ({ formState: prevFormState, submitted }) => {
const controller = handleAbortRef(abortOnChangeRef)
const currentTime = Date.now()
@@ -351,6 +352,7 @@ export const DefaultEditView: React.FC<ClientSideEditViewProps> = ({
formState: prevFormState,
globalSlug,
operation,
skipValidation: !submitted,
// Performance optimization: Setting it to false ensure that only fields that have explicit requireRender set in the form state will be rendered (e.g. new array rows).
// We only want to render ALL fields on initial render, not in onChange.
renderAllFields: false,

View File

@@ -34,7 +34,7 @@ const collectionWithName = (collectionSlug: string): CollectionConfig => {
}
}
export const slug = 'posts'
export const postsSlug = 'posts'
export const relationSlug = 'relation'
export const pointSlug = 'point'
export const customIdSlug = 'custom-id'
@@ -51,7 +51,7 @@ export default buildConfigWithDefaults({
},
collections: [
{
slug,
slug: postsSlug,
access: openAccess,
fields: [
{
@@ -346,14 +346,14 @@ export default buildConfigWithDefaults({
// Relation - hasMany
await payload.create({
collection: slug,
collection: postsSlug,
data: {
relationHasManyField: rel1.id,
title: 'rel to hasMany',
},
})
await payload.create({
collection: slug,
collection: postsSlug,
data: {
relationHasManyField: rel2.id,
title: 'rel to hasMany 2',
@@ -362,7 +362,7 @@ export default buildConfigWithDefaults({
// Relation - relationTo multi
await payload.create({
collection: slug,
collection: postsSlug,
data: {
relationMultiRelationTo: {
relationTo: relationSlug,
@@ -374,7 +374,7 @@ export default buildConfigWithDefaults({
// Relation - relationTo multi hasMany
await payload.create({
collection: slug,
collection: postsSlug,
data: {
relationMultiRelationToHasMany: [
{

View File

@@ -18,7 +18,7 @@ import {
methods,
pointSlug,
relationSlug,
slug,
postsSlug,
} from './config.js'
const filename = fileURLToPath(import.meta.url)
@@ -55,7 +55,7 @@ describe('collections-rest', () => {
it('should find', async () => {
const post1 = await createPost()
const post2 = await createPost()
const response = await restClient.GET(`/${slug}`)
const response = await restClient.GET(`/${postsSlug}`)
const result = await response.json()
expect(response.status).toEqual(200)
@@ -68,7 +68,7 @@ describe('collections-rest', () => {
it('should count', async () => {
await createPost()
await createPost()
const response = await restClient.GET(`/${slug}/count`)
const response = await restClient.GET(`/${postsSlug}/count`)
const result = await response.json()
expect(response.status).toEqual(200)
@@ -78,7 +78,7 @@ describe('collections-rest', () => {
it('should find where id', async () => {
const post1 = await createPost()
await createPost()
const response = await restClient.GET(`/${slug}`, {
const response = await restClient.GET(`/${postsSlug}`, {
query: {
where: { id: { equals: post1.id } },
},
@@ -95,7 +95,7 @@ describe('collections-rest', () => {
const post2 = await createPost()
const { docs, totalDocs } = await payload.find({
collection: slug,
collection: postsSlug,
overrideAccess: false,
pagination: false,
})
@@ -111,7 +111,7 @@ describe('collections-rest', () => {
const { id, description } = await createPost({ description: 'desc' })
const updatedTitle = 'updated-title'
const response = await restClient.PATCH(`/${slug}/${id}`, {
const response = await restClient.PATCH(`/${postsSlug}/${id}`, {
body: JSON.stringify({ title: updatedTitle }),
})
const { doc } = await response.json()
@@ -128,7 +128,7 @@ describe('collections-rest', () => {
}
const description = 'updated'
const response = await restClient.PATCH(`/${slug}`, {
const response = await restClient.PATCH(`/${postsSlug}`, {
body: JSON.stringify({
description,
}),
@@ -151,7 +151,7 @@ describe('collections-rest', () => {
}
const description = 'updated-description'
const response = await restClient.PATCH(`/${slug}`, {
const response = await restClient.PATCH(`/${postsSlug}`, {
body: JSON.stringify({
description,
}),
@@ -167,7 +167,7 @@ describe('collections-rest', () => {
const { docs: resDocs } = await payload.find({
limit: 10,
collection: slug,
collection: postsSlug,
where: { id: { in: ids } },
})
expect(resDocs.at(-1).description).toEqual('to-update')
@@ -180,7 +180,7 @@ describe('collections-rest', () => {
const description = 'updated'
const response = await restClient.PATCH(`/${slug}`, {
const response = await restClient.PATCH(`/${postsSlug}`, {
body: JSON.stringify({
description,
}),
@@ -193,7 +193,7 @@ describe('collections-rest', () => {
expect(errors).toHaveLength(1)
const { docs } = await payload.find({
collection: slug,
collection: postsSlug,
})
expect(docs[0].description).not.toEqual(description)
@@ -206,7 +206,7 @@ describe('collections-rest', () => {
}
const description = 'updated'
const relationFieldResponse = await restClient.PATCH(`/${slug}`, {
const relationFieldResponse = await restClient.PATCH(`/${postsSlug}`, {
body: JSON.stringify({
description,
}),
@@ -214,7 +214,7 @@ describe('collections-rest', () => {
})
expect(relationFieldResponse.status).toEqual(400)
const relationMultiRelationToResponse = await restClient.PATCH(`/${slug}`, {
const relationMultiRelationToResponse = await restClient.PATCH(`/${postsSlug}`, {
body: JSON.stringify({
description,
}),
@@ -223,7 +223,7 @@ describe('collections-rest', () => {
expect(relationMultiRelationToResponse.status).toEqual(400)
const { docs } = await payload.find({
collection: slug,
collection: postsSlug,
})
expect(docs[0].description).not.toEqual(description)
@@ -232,14 +232,14 @@ describe('collections-rest', () => {
it('should not bulk update with a read restricted field query', async () => {
const { id } = await payload.create({
collection: slug,
collection: postsSlug,
data: {
restrictedField: 'restricted',
},
})
const description = 'description'
const response = await restClient.PATCH(`/${slug}`, {
const response = await restClient.PATCH(`/${postsSlug}`, {
body: JSON.stringify({
description,
}),
@@ -249,7 +249,7 @@ describe('collections-rest', () => {
const doc = await payload.findByID({
id,
collection: slug,
collection: postsSlug,
})
expect(response.status).toEqual(400)
@@ -301,7 +301,7 @@ describe('collections-rest', () => {
await createPost({ description: `desc ${i}` })
}
const response = await restClient.DELETE(`/${slug}`, {
const response = await restClient.DELETE(`/${postsSlug}`, {
query: { where: { title: { equals: 'title' } } },
})
const { docs } = await response.json()
@@ -481,7 +481,7 @@ describe('collections-rest', () => {
it('should delete', async () => {
const { id } = await createPost()
const response = await restClient.DELETE(`/${slug}/${id}`)
const response = await restClient.DELETE(`/${postsSlug}/${id}`)
const { doc } = await response.json()
expect(response.status).toEqual(200)
@@ -491,7 +491,7 @@ describe('collections-rest', () => {
it('should include metadata', async () => {
await createPosts(11)
const result = await restClient.GET(`/${slug}`).then((res) => res.json())
const result = await restClient.GET(`/${postsSlug}`).then((res) => res.json())
expect(result.totalDocs).toBeGreaterThan(0)
expect(result.limit).toBe(10)
@@ -534,7 +534,7 @@ describe('collections-rest', () => {
describe('regular relationship', () => {
it('query by property value', async () => {
const response = await restClient.GET(`/${slug}`, {
const response = await restClient.GET(`/${postsSlug}`, {
query: {
where: { relationField: { equals: relation.id } },
},
@@ -547,7 +547,7 @@ describe('collections-rest', () => {
})
it('should count query by property value', async () => {
const response = await restClient.GET(`/${slug}/count`, {
const response = await restClient.GET(`/${postsSlug}/count`, {
query: {
where: { relationField: { equals: relation.id } },
},
@@ -559,7 +559,7 @@ describe('collections-rest', () => {
})
it('query by id', async () => {
const response = await restClient.GET(`/${slug}`, {
const response = await restClient.GET(`/${postsSlug}`, {
query: {
where: { relationField: { equals: relation.id } },
},
@@ -573,13 +573,13 @@ describe('collections-rest', () => {
it('should query LIKE by ID', async () => {
const post = await payload.create({
collection: slug,
collection: postsSlug,
data: {
title: 'find me buddy',
},
})
const response = await restClient.GET(`/${slug}`, {
const response = await restClient.GET(`/${postsSlug}`, {
query: {
where: {
id: {
@@ -600,7 +600,7 @@ describe('collections-rest', () => {
relationHasManyField: [relation.id, relation2.id],
})
const response = await restClient.GET(`/${slug}`, {
const response = await restClient.GET(`/${postsSlug}`, {
query: {
where: { 'relationHasManyField.name': { equals: relation.name } },
},
@@ -612,7 +612,7 @@ describe('collections-rest', () => {
expect(result.totalDocs).toEqual(1)
// Query second relationship
const response2 = await restClient.GET(`/${slug}`, {
const response2 = await restClient.GET(`/${postsSlug}`, {
query: {
where: { 'relationHasManyField.name': { equals: relation2.name } },
},
@@ -631,7 +631,7 @@ describe('collections-rest', () => {
})
await createPost()
const response = await restClient.GET(`/${slug}`, {
const response = await restClient.GET(`/${postsSlug}`, {
query: {
where: { 'relationMultiRelationTo.value': { equals: relation.id } },
},
@@ -650,7 +650,7 @@ describe('collections-rest', () => {
})
await createPost()
const response = await restClient.GET(`/${slug}`, {
const response = await restClient.GET(`/${postsSlug}`, {
query: {
where: {
and: [
@@ -678,7 +678,7 @@ describe('collections-rest', () => {
})
await createPost()
const response = await restClient.GET(`/${slug}`, {
const response = await restClient.GET(`/${postsSlug}`, {
query: {
where: { 'relationMultiRelationToHasMany.value': { equals: relation.id } },
},
@@ -690,7 +690,7 @@ describe('collections-rest', () => {
expect(result.totalDocs).toEqual(1)
// Query second relation
const response2 = await restClient.GET(`/${slug}`, {
const response2 = await restClient.GET(`/${postsSlug}`, {
query: {
where: { 'relationMultiRelationToHasMany.value': { equals: relation.id } },
},
@@ -711,7 +711,7 @@ describe('collections-rest', () => {
const test = 'test'
await createPost({ fakeLocalization: test })
const response = await restClient.GET(`/${slug}`, {
const response = await restClient.GET(`/${postsSlug}`, {
query: {
where: { fakeLocalization: { equals: test } },
},
@@ -723,7 +723,7 @@ describe('collections-rest', () => {
})
it('should not error when attempting to sort on a field that does not exist', async () => {
const response = await restClient.GET(`/${slug}`, {
const response = await restClient.GET(`/${postsSlug}`, {
query: {
sort: 'fake',
},
@@ -738,7 +738,7 @@ describe('collections-rest', () => {
const valueToQuery = 'valueToQuery'
const post1 = await createPost({ title: valueToQuery })
await createPost()
const response = await restClient.GET(`/${slug}`, {
const response = await restClient.GET(`/${postsSlug}`, {
query: {
where: { title: { equals: valueToQuery } },
},
@@ -754,7 +754,7 @@ describe('collections-rest', () => {
const post1 = await createPost({ title: 'not-equals' })
const post2 = await createPost()
const post3 = await createPost({ title: undefined })
const response = await restClient.GET(`/${slug}`, {
const response = await restClient.GET(`/${postsSlug}`, {
query: {
where: { title: { not_equals: post1.title } },
},
@@ -769,7 +769,7 @@ describe('collections-rest', () => {
it('in', async () => {
const post1 = await createPost({ title: 'my-title' })
await createPost()
const response = await restClient.GET(`/${slug}`, {
const response = await restClient.GET(`/${postsSlug}`, {
query: {
where: { title: { in: [post1.title] } },
},
@@ -784,7 +784,7 @@ describe('collections-rest', () => {
it('not_in', async () => {
const post1 = await createPost({ title: 'not-me' })
const post2 = await createPost()
const response = await restClient.GET(`/${slug}`, {
const response = await restClient.GET(`/${postsSlug}`, {
query: {
where: { title: { not_in: [post1.title] } },
},
@@ -805,7 +805,7 @@ describe('collections-rest', () => {
await createPost({ relationField: relationship.id, title: 'not-me' })
// await createPost({ relationMultiRelationTo: relationship.id, title: 'not-me' })
const post2 = await createPost({ title: 'me' })
const response = await restClient.GET(`/${slug}`, {
const response = await restClient.GET(`/${postsSlug}`, {
query: {
where: { relationField: { not_in: [relationship.id] } },
},
@@ -817,7 +817,7 @@ describe('collections-rest', () => {
expect(result.totalDocs).toEqual(1)
// do not want to error for empty arrays
const emptyNotInResponse = await restClient.GET(`/${slug}`, {
const emptyNotInResponse = await restClient.GET(`/${postsSlug}`, {
query: {
where: { relationField: { not_in: [] } },
},
@@ -835,7 +835,7 @@ describe('collections-rest', () => {
const post1 = await createPost({ relationField: relationship.id, title: 'me' })
// await createPost({ relationMultiRelationTo: relationship.id, title: 'not-me' })
await createPost({ title: 'not-me' })
const response = await restClient.GET(`/${slug}`, {
const response = await restClient.GET(`/${postsSlug}`, {
query: {
where: { relationField: { in: [relationship.id] } },
},
@@ -847,7 +847,7 @@ describe('collections-rest', () => {
expect(result.totalDocs).toEqual(1)
// do not want to error for empty arrays
const emptyNotInResponse = await restClient.GET(`/${slug}`, {
const emptyNotInResponse = await restClient.GET(`/${postsSlug}`, {
query: {
where: { relationField: { in: [] } },
},
@@ -859,7 +859,7 @@ describe('collections-rest', () => {
it('like', async () => {
const post1 = await createPost({ title: 'prefix-value' })
const response = await restClient.GET(`/${slug}`, {
const response = await restClient.GET(`/${postsSlug}`, {
query: {
where: { title: { like: 'prefix' } },
},
@@ -881,7 +881,7 @@ describe('collections-rest', () => {
title: specialCharacters,
})
const response = await restClient.GET(`/${slug}`, {
const response = await restClient.GET(`/${postsSlug}`, {
query: {
where: {
title: {
@@ -902,7 +902,7 @@ describe('collections-rest', () => {
it('like - cyrillic characters', async () => {
const post1 = await createPost({ title: 'Тест' })
const response = await restClient.GET(`/${slug}`, {
const response = await restClient.GET(`/${postsSlug}`, {
query: {
where: {
title: {
@@ -921,7 +921,7 @@ describe('collections-rest', () => {
it('like - cyrillic characters in multiple words', async () => {
const post1 = await createPost({ title: 'привет, это тест полезной нагрузки' })
const response = await restClient.GET(`/${slug}`, {
const response = await restClient.GET(`/${postsSlug}`, {
query: {
where: {
title: {
@@ -939,7 +939,7 @@ describe('collections-rest', () => {
it('like - partial word match', async () => {
const post = await createPost({ title: 'separate words should partially match' })
const response = await restClient.GET(`/${slug}`, {
const response = await restClient.GET(`/${postsSlug}`, {
query: {
where: {
title: {
@@ -958,7 +958,7 @@ describe('collections-rest', () => {
it('like - id should not crash', async () => {
const post = await createPost({ title: 'post' })
const response = await restClient.GET(`/${slug}`, {
const response = await restClient.GET(`/${postsSlug}`, {
query: {
where: {
id: {
@@ -974,7 +974,7 @@ describe('collections-rest', () => {
it('exists - true', async () => {
const postWithDesc = await createPost({ description: 'exists' })
await createPost({ description: undefined })
const response = await restClient.GET(`/${slug}`, {
const response = await restClient.GET(`/${postsSlug}`, {
query: {
where: {
description: {
@@ -993,7 +993,7 @@ describe('collections-rest', () => {
it('exists - false', async () => {
const postWithoutDesc = await createPost({ description: undefined })
await createPost({ description: 'exists' })
const response = await restClient.GET(`/${slug}`, {
const response = await restClient.GET(`/${postsSlug}`, {
query: {
where: {
description: {
@@ -1018,7 +1018,7 @@ describe('collections-rest', () => {
})
it('greater_than', async () => {
const response = await restClient.GET(`/${slug}`, {
const response = await restClient.GET(`/${postsSlug}`, {
query: {
where: {
number: {
@@ -1035,7 +1035,7 @@ describe('collections-rest', () => {
})
it('greater_than_equal', async () => {
const response = await restClient.GET(`/${slug}`, {
const response = await restClient.GET(`/${postsSlug}`, {
query: {
where: {
number: {
@@ -1054,7 +1054,7 @@ describe('collections-rest', () => {
})
it('less_than', async () => {
const response = await restClient.GET(`/${slug}`, {
const response = await restClient.GET(`/${postsSlug}`, {
query: {
where: {
number: {
@@ -1071,7 +1071,7 @@ describe('collections-rest', () => {
})
it('less_than_equal', async () => {
const response = await restClient.GET(`/${slug}`, {
const response = await restClient.GET(`/${postsSlug}`, {
query: {
where: {
number: {
@@ -1312,7 +1312,7 @@ describe('collections-rest', () => {
const post2 = await createPost({ title: 'post2' })
await createPost()
const response = await restClient.GET(`/${slug}`, {
const response = await restClient.GET(`/${postsSlug}`, {
query: {
where: {
or: [
@@ -1342,7 +1342,7 @@ describe('collections-rest', () => {
const post1 = await createPost({ title: 'post1' })
await createPost()
const response = await restClient.GET(`/${slug}`, {
const response = await restClient.GET(`/${postsSlug}`, {
query: {
where: {
or: [
@@ -1374,7 +1374,7 @@ describe('collections-rest', () => {
await createPost({ description, title: 'post2' }) // Diff title, same desc
await createPost()
const response = await restClient.GET(`/${slug}`, {
const response = await restClient.GET(`/${postsSlug}`, {
query: {
where: {
and: [
@@ -1427,13 +1427,13 @@ describe('collections-rest', () => {
},
},
}
let response = await restClient.GET(`/${slug}`, { query })
let response = await restClient.GET(`/${postsSlug}`, { query })
const page1 = await response.json()
response = await restClient.GET(`/${slug}`, { query: { ...query, page: 2 } })
response = await restClient.GET(`/${postsSlug}`, { query: { ...query, page: 2 } })
const page2 = await response.json()
response = await restClient.GET(`/${slug}`, { query: { ...query, page: 3 } })
response = await restClient.GET(`/${postsSlug}`, { query: { ...query, page: 3 } })
const page3 = await response.json()
expect(page1.hasNextPage).toStrictEqual(true)
@@ -1476,13 +1476,13 @@ describe('collections-rest', () => {
},
},
}
let response = await restClient.GET(`/${slug}`, { query })
let response = await restClient.GET(`/${postsSlug}`, { query })
const page1 = await response.json()
response = await restClient.GET(`/${slug}`, { query: { ...query, page: 2 } })
response = await restClient.GET(`/${postsSlug}`, { query: { ...query, page: 2 } })
const page2 = await response.json()
response = await restClient.GET(`/${slug}`, { query: { ...query, page: 3 } })
response = await restClient.GET(`/${postsSlug}`, { query: { ...query, page: 3 } })
const page3 = await response.json()
expect(page1.hasNextPage).toStrictEqual(true)
@@ -1524,7 +1524,7 @@ describe('collections-rest', () => {
})
it('should query a limited set of docs', async () => {
const response = await restClient.GET(`/${slug}`, {
const response = await restClient.GET(`/${postsSlug}`, {
query: {
limit: 15,
where: {
@@ -1541,7 +1541,7 @@ describe('collections-rest', () => {
})
it('should query all docs when limit=0', async () => {
const response = await restClient.GET(`/${slug}`, {
const response = await restClient.GET(`/${postsSlug}`, {
query: {
limit: 0,
where: {
@@ -1566,7 +1566,7 @@ describe('collections-rest', () => {
})
const result = await restClient
.GET(`/${slug}`, {
.GET(`/${postsSlug}`, {
query: {
where: {
'D1.D2.D3.D4': {
@@ -1641,11 +1641,11 @@ describe('collections-rest', () => {
const post = await createPost({})
const response = await restClient.GET(
`/${slug}/${typeof post.id === 'number' ? 1000 : randomUUID()}`,
`/${postsSlug}/${typeof post.id === 'number' ? 1000 : randomUUID()}`,
)
expect(response.status).toBe(404)
expect(collection.slug).toBe(slug)
expect(collection.slug).toBe(postsSlug)
expect(err).toBeInstanceOf(NotFound)
expect(errResult).toStrictEqual({
errors: [
@@ -1660,6 +1660,32 @@ describe('collections-rest', () => {
payload.collections.posts.config.hooks.afterError = []
})
it('should return field-level validation errors', async () => {
let errorMessage: string
try {
const result = await payload.create({
collection: postsSlug,
data: {
D1: {
D2: {
D3: {
// @ts-expect-error
D4: {},
},
},
},
},
})
} catch (e) {
errorMessage = e.message
}
await expect(errorMessage).toBe(
'posts validation failed: D1.D2.D3.D4: Cast to string failed for value "{}" (type Object) at path "D4"',
)
})
})
describe('Local', () => {
@@ -1695,7 +1721,7 @@ describe('collections-rest', () => {
async function createPost(overrides?: Partial<Post>) {
const { doc } = await restClient
.POST(`/${slug}`, {
.POST(`/${postsSlug}`, {
body: JSON.stringify({ title: 'title', ...overrides }),
})
.then((res) => res.json())
@@ -1710,7 +1736,7 @@ async function createPosts(count: number) {
async function clearDocs(): Promise<void> {
await payload.delete({
collection: slug,
collection: postsSlug,
where: { id: { exists: true } },
})
}

View File

@@ -28,7 +28,7 @@
}
],
"paths": {
"@payload-config": ["./test/field-perf/config.ts"],
"@payload-config": ["./test/collections-rest/config.ts"],
"@payloadcms/live-preview": ["./packages/live-preview/src"],
"@payloadcms/live-preview-react": ["./packages/live-preview-react/src/index.ts"],
"@payloadcms/live-preview-vue": ["./packages/live-preview-vue/src/index.ts"],