jquense / yup

Dead simple Object schema validation
MIT License
22.94k stars 935 forks source link

Shape and array of after upgrade #2081

Open AuthorProxy opened 1 year ago

AuthorProxy commented 1 year ago

How it was before upgrade on 0.x.x version:

const playersSchema = yup
  .array()
  .of(yup.object<Shape<Player>>())
  .test("isUniquePlayers", (players, ctx) => { ... }

where Shape is: export type Shape<T> = Partial<Record<keyof T, AnySchema>>;

How I fix afterupgrade errors on 1.x.x version: 1st variant:

const playersSchema = yup
  .array()
  .of(yup.object().shape<Shape<Player>>({}))
  .test("isUniquePlayers", (players, ctx) => { ... }

2nd variant:

const playersSchema = yup
  .array()
  .of<Shape<Player>>({} as yup.ISchema<Shape<Player>, yup.AnyObject>)
  .test("isUniquePlayers", (players, ctx) => { ... }

Can you help me plz this part of code without using typescript as? If the 1variant good or any other way to achive this?

AuthorProxy commented 1 year ago

I then got players type in the test function isUniquePlayers like this:

(parameter) players: {
    __typename?: unknown;
    project_id?: unknown;
    certificate_id?: unknown;
    name?: unknown;

how can I get a normal type for players object without optional keys, unknown values and typescript as?!

jquense commented 1 year ago

You shouldn't need to fill the generics there, just remove them and let the schema determine the type. If you want to ensure things match a given interface then follow the docs for typescript usage and the migration issue pinned here

gmonitoring commented 1 year ago

Вам не нужно заполнять туда дженерики, просто удалите их и позвольте схеме определить тип. Если вы хотите убедиться, что все соответствует данному интерфейсу, следуйте документации по использованию машинописного текста и проблеме миграции, закрепленной здесь.

Could you provide a link showing how to type yup schema based on interface?

AuthorProxy commented 1 year ago

You shouldn't need to fill the generics there, just remove them and let the schema determine the type. If you want to ensure things match a given interface then follow the docs for typescript usage and the migration issue pinned here

There is nothing about this part at migration guide, thats why i-am asking!!!