Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Field is set to nullable in types when the required option is true and an admin condition is configured. #9291

Open
bouchaala-sabri opened this issue Nov 18, 2024 · 3 comments
Labels
status: needs-triage Possible bug which hasn't been reproduced yet

Comments

@bouchaala-sabri
Copy link

Describe the Bug

When a field of a relationship type is marked as required, but an admin condition is present, the generated types sets the field to nullable.
Admin UI and API validation will return an error if condition none fulfilled but the generated types set the field as optional.

Link to the code that reproduces this issue

https://github.com/bouchaala-sabri/payload

Reproduction Steps

  • Create 2 collections A and B
  • Collection A have a field type "relationship" of B
  • Set it to required have and admin condition, you can set it the condition to "true" , "false" or any boolean condition. The bug happens in all cases.

Example

Collection Post

import type { CollectionConfig } from 'payload'

export const postsSlug = 'posts'

export const PostsCollection: CollectionConfig = {
  slug: postsSlug,
  admin: {
    useAsTitle: 'title',
  },
  fields: [
    {
      name: 'title',
      type: 'text',
    },
    {
      name: 'category',
      type: 'relationship',
      relationTo: 'categories',
      required: true,
      admin: {
        condition: (data) => true,
      },
      label: 'Segment',
    },
  ],
  versions: {
    drafts: true,
  },
}

Collection Categories

import type { CollectionConfig } from 'payload'

export const CategoriesCollection: CollectionConfig = {
  slug: 'categories',
  admin: {
    useAsTitle: 'name',
  },
  fields: [{ name: 'name', type: 'text', required: true }],
}

Generated types

export interface Post {
  id: string;
  title?: string | null;
  category?: (string | null) | Category;
  updatedAt: string;
  createdAt: string;
  _status?: ('draft' | 'published') | null;
}

Which area(s) are affected? (Select all that apply)

Not sure

Environment Info

Payload: 3.0.0-beta.134
next: 15.0.0
node: v22.10.0
@qgwr32
Copy link

qgwr32 commented Nov 19, 2024

If condition returns false, no relationship must be specified, therefore the field is nullable.
Looks fine to me.

@denolfe denolfe removed the v3 label Nov 19, 2024
@bouchaala-sabri
Copy link
Author

@qgwr32 In my real case, the condition is related to a another required fields, for example a propriety have a country and a city, both required and the city will not be shown if no country was set. I tested it in both UI and API and there is no case were a propriety can be created without a city, whatever the condition is.

@qgwr32
Copy link

qgwr32 commented Nov 20, 2024

@bouchaala-sabri I get what you are saying, payload cannot however deduce that relationship as it does not know what condition actually depends on.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: needs-triage Possible bug which hasn't been reproduced yet
Projects
None yet
Development

No branches or pull requests

3 participants