Skip to content

Invalid constraints for non-nullable array #489

Open
@rwysocki-equinix

Description

@rwysocki-equinix

When array elements are not nullable, the array validation constraints are applied not to array, but each element
I've modified an existing zod testcase which now fails to visualize this problem:

  it('list field non-null', async () => {
    const schema = buildSchema(/* GraphQL */ `
      input UserCreateInput {
        profile: [String!]! @constraint(minLength: 1, maxLength: 5000)
      }

      directive @constraint(minLength: Int!, maxLength: Int!) on INPUT_FIELD_DEFINITION
    `);
    const result = await plugin(
        schema,
        [],
        {
          schema: 'zod',
          directives: {
            constraint: {
              minLength: ['min', '$1', 'Please input more than $1'],
              maxLength: ['max', '$1', 'Please input less than $1'],
            },
          },
        },
        {}
    );
    const wantContains = [
      'export function UserCreateInputSchema(): z.ZodObject<Properties<UserCreateInput>>',
      'profile: z.array(z.string()).min(1, "Please input more than 1").max(5000, "Please input less than 5000")',
    ];
    for (const wantContain of wantContains) {
      expect(result.content).toContain(wantContain);
    }
  });

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions