Skip to content

incorrect default value for an array with minItems and no default for the items #3656

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

Closed
4 tasks done
anthonycaron opened this issue May 10, 2023 · 4 comments
Closed
4 tasks done
Labels
awaiting response bug possibly close To confirm if this issue can be closed

Comments

@anthonycaron
Copy link
Contributor

Prerequisites

What theme are you using?

core

Version

latest

Current Behavior

Given this schema:

const schema: RJSFSchema = {
  type: 'object',
  properties: {
    optionalArray: {
      type: 'array',
      minItems: 2,
      items: { type: 'string' },
    },
  },
};

And given the raw form data with a value of {},
I believe the resulting data after processing the default values should be { optionalArray: undefined } since the optionalArray property is declared in the schema hence I guess we expect to have in our resulting data, but with a value of undefined since it's optional. ajv would consider the data as valid in that case.

As you can see in the playground, the value returned is:

{
  "optionalArray": [
    null,
    null
  ]
}

And when running the following test

it('should return partial array for an optional array property with minItems when provided with a partial array', () => {
  const schema: RJSFSchema = {
    type: 'object',
    properties: {
      optionalArray: {
        type: 'array',
        minItems: 2,
        items: { type: 'string' },
      },
    },
  };
  expect(
    computeDefaults(testValidator, schema, {
      rootSchema: schema,
      rawFormData: {},
      behaviorBitFlags: DefaultFormStateBehavior.IgnoreMinItemsUnlessRequired,
    })
  ).toEqual({ optionalArray: undefined });
});

in getDefaultFormStateTest.ts, we have the following result:

{
  "optionalArray": [
    undefined,
    undefined
  ]
}

I guess this is because I'm running this test on the latest version of the codebase and the playground might be using the latest release.
In any case, the issue is there.

Expected Behavior

This test should pass:

it('should return partial array for an optional array property with minItems when provided with a partial array', () => {
  const schema: RJSFSchema = {
    type: 'object',
    properties: {
      optionalArray: {
        type: 'array',
        minItems: 2,
        items: { type: 'string' },
      },
    },
  };
  expect(
    computeDefaults(testValidator, schema, {
      rootSchema: schema,
      rawFormData: {},
      behaviorBitFlags: DefaultFormStateBehavior.IgnoreMinItemsUnlessRequired,
    })
  ).toEqual({ optionalArray: undefined });
});

Steps To Reproduce

You can try it in the playground

Or

  1. fork the repo
  2. run npm i
  3. add the test I put up here in the packages/utils/test/schema/getDefaultFormStateTest.ts file
  4. run npm run test

Environment

- OS: MacOS Ventura
- Node: 18.7.0
- npm: 8.15.0

Anything else?

No response

@anthonycaron anthonycaron added bug needs triage Initial label given, to be assigned correct labels and assigned labels May 10, 2023
@anthonycaron anthonycaron changed the title default value of an optional array with minItems and no default for the items default value of an optional array with minItems and no default for the items is incorrect May 10, 2023
@anthonycaron anthonycaron changed the title default value of an optional array with minItems and no default for the items is incorrect incorrect default value for an optional array with minItems and no default for the items May 10, 2023
@anthonycaron
Copy link
Contributor Author

Just for context, there's a discussion regarding this here.

@anthonycaron anthonycaron changed the title incorrect default value for an optional array with minItems and no default for the items incorrect default value for an array with minItems and no default for the items May 11, 2023
@heath-freenome
Copy link
Member

heath-freenome commented May 12, 2023

@anthonycaron The playground editors will convert the [undefined, undefined] array into [null, null] so that is expected behavior. Also, once we release 5.7.0 (soon!), the new experimental_DefaultFormStateBehavior.arrayMinItems: 'requiredOnly' flag being added to the Form could also fix this. Would you be willing to take another look at this once we release?

@heath-freenome heath-freenome added awaiting response possibly close To confirm if this issue can be closed and removed needs triage Initial label given, to be assigned correct labels and assigned labels May 12, 2023
@anthonycaron
Copy link
Contributor Author

Sure, thank you !

@stale
Copy link

stale bot commented Jun 12, 2023

This issue was closed because of lack of recent activity. Reoopen if you still need assistance.

@stale stale bot closed this as completed Jun 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting response bug possibly close To confirm if this issue can be closed
Projects
None yet
Development

No branches or pull requests

2 participants