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

Typescript: Initial Values PartialDeep not working as expected #4870

Open
5 tasks done
robinpilz opened this issue Sep 25, 2024 · 0 comments
Open
5 tasks done

Typescript: Initial Values PartialDeep not working as expected #4870

robinpilz opened this issue Sep 25, 2024 · 0 comments

Comments

@robinpilz
Copy link

What happened?

Typescript Error is thrown for initial values when a property in the schema is required. This should not be the case as the input values should always be able to be initialized as undefined (empty).

2024-09-25 16_37_44-test vue - ecosys (Workspace)  WSL_ Ubuntu-20 04  - Visual Studio Code

I found that for regular objects the PartialDeep seems to work (so all the properties are optional), but when having an array of objects typescript complains that all the required values must be present when initializing

Reproduction steps

Example which shows error for undefined values (more complex schema):

<script lang="ts" setup>
import { toTypedSchema } from '@vee-validate/yup';
import { useForm } from 'vee-validate';
import { array, number, object, string } from 'yup';

const getInitialData = () => {
  return {
    array: [
      {
        someText: 'String',
      },
    ],
  };
};

const schema = toTypedSchema(
  object({
    array: array()
      .of(
        object({
          someText: string().required(),
          someId: number().required(),
        }),
      )
      .min(1),
  }),
);

const { resetForm } = useForm({
  validationSchema: schema,
  initialValues: getInitialData(),
});

const reset = () => {
  resetForm(
    {
      values: getInitialData(),
    },
    { force: true },
  );
};
</script>

<template>
  <div></div>
</template>

Example of working version (simple object schema):

<script lang="ts" setup>
import { toTypedSchema } from '@vee-validate/yup';
import { useForm } from 'vee-validate';
import { number, object, string } from 'yup';

const getInitialData = () => {
  return {
    someText: 'String',
  };
};

const schema = toTypedSchema(
  object({
    someText: string().required(),
    someId: number().required(),
  }),
);

const { resetForm } = useForm({
  validationSchema: schema,
  initialValues: getInitialData(),
});

const reset = () => {
  resetForm(
    {
      values: getInitialData(),
    },
    { force: true },
  );
};
</script>

<template>
  <div></div>
</template>

Version

Vue.js 3.x and vee-validate 4.x

What browsers are you seeing the problem on?

  • Firefox
  • Chrome
  • Safari
  • Microsoft Edge

Relevant log output

No response

Demo link

  • Could not get typescript to show errors in sandboxes -

Code of Conduct

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant