|
1 | 1 | import { Ref } from 'vue'; |
2 | 2 | import { useField, useForm } from '@/vee-validate'; |
3 | | -import { string, minLength, email as emailV, object, coerce, any, number, withDefault, optional } from 'valibot'; |
| 3 | +import { string, minLength, email as emailV, object, coerce, any, number, withDefault, optional, array } from 'valibot'; |
4 | 4 | import { toTypedSchema } from '@/valibot'; |
5 | 5 | import { mountWithHoc, flushPromises, setValue } from '../../vee-validate/tests/helpers'; |
6 | 6 |
|
@@ -365,3 +365,110 @@ describe('valibot', () => { |
365 | 365 | await expect(initialSpy).toHaveBeenLastCalledWith(expect.objectContaining({})); |
366 | 366 | }); |
367 | 367 | }); |
| 368 | + |
| 369 | +test('reports required state on fields', async () => { |
| 370 | + const metaSpy = vi.fn(); |
| 371 | + mountWithHoc({ |
| 372 | + setup() { |
| 373 | + const schema = toTypedSchema( |
| 374 | + object({ |
| 375 | + 'not.nested.path': string(), |
| 376 | + name: optional(string()), |
| 377 | + email: string(), |
| 378 | + nested: object({ |
| 379 | + arr: array(object({ req: string(), nreq: optional(string()) })), |
| 380 | + obj: object({ |
| 381 | + req: string(), |
| 382 | + nreq: optional(string()), |
| 383 | + }), |
| 384 | + }), |
| 385 | + }), |
| 386 | + ); |
| 387 | + |
| 388 | + useForm({ |
| 389 | + validationSchema: schema, |
| 390 | + }); |
| 391 | + |
| 392 | + const { meta: name } = useField('name'); |
| 393 | + const { meta: email } = useField('email'); |
| 394 | + const { meta: req } = useField('nested.obj.req'); |
| 395 | + const { meta: nreq } = useField('nested.obj.nreq'); |
| 396 | + const { meta: arrReq } = useField('nested.arr.0.req'); |
| 397 | + const { meta: arrNreq } = useField('nested.arr.1.nreq'); |
| 398 | + const { meta: notNested } = useField('[not.nested.path]'); |
| 399 | + |
| 400 | + metaSpy({ |
| 401 | + name: name.required, |
| 402 | + email: email.required, |
| 403 | + objReq: req.required, |
| 404 | + objNreq: nreq.required, |
| 405 | + arrReq: arrReq.required, |
| 406 | + arrNreq: arrNreq.required, |
| 407 | + notNested: notNested.required, |
| 408 | + }); |
| 409 | + |
| 410 | + return { |
| 411 | + schema, |
| 412 | + }; |
| 413 | + }, |
| 414 | + template: `<div></div>`, |
| 415 | + }); |
| 416 | + |
| 417 | + await flushPromises(); |
| 418 | + await expect(metaSpy).toHaveBeenLastCalledWith( |
| 419 | + expect.objectContaining({ |
| 420 | + name: false, |
| 421 | + email: true, |
| 422 | + objReq: true, |
| 423 | + objNreq: false, |
| 424 | + arrReq: true, |
| 425 | + arrNreq: false, |
| 426 | + notNested: true, |
| 427 | + }), |
| 428 | + ); |
| 429 | +}); |
| 430 | + |
| 431 | +test('reports required false for non-existent fields', async () => { |
| 432 | + const metaSpy = vi.fn(); |
| 433 | + mountWithHoc({ |
| 434 | + setup() { |
| 435 | + const schema = toTypedSchema( |
| 436 | + object({ |
| 437 | + name: string(), |
| 438 | + nested: object({ |
| 439 | + arr: array(object({ prop: string() })), |
| 440 | + obj: object({}), |
| 441 | + }), |
| 442 | + }), |
| 443 | + ); |
| 444 | + |
| 445 | + useForm({ |
| 446 | + validationSchema: schema, |
| 447 | + }); |
| 448 | + |
| 449 | + const { meta: email } = useField('email'); |
| 450 | + const { meta: req } = useField('nested.obj.req'); |
| 451 | + const { meta: arrReq } = useField('nested.arr.0.req'); |
| 452 | + |
| 453 | + metaSpy({ |
| 454 | + email: email.required, |
| 455 | + objReq: req.required, |
| 456 | + arrReq: arrReq.required, |
| 457 | + }); |
| 458 | + |
| 459 | + return { |
| 460 | + schema, |
| 461 | + }; |
| 462 | + }, |
| 463 | + template: `<div></div>`, |
| 464 | + }); |
| 465 | + |
| 466 | + await flushPromises(); |
| 467 | + await expect(metaSpy).toHaveBeenLastCalledWith( |
| 468 | + expect.objectContaining({ |
| 469 | + email: false, |
| 470 | + objReq: false, |
| 471 | + arrReq: false, |
| 472 | + }), |
| 473 | + ); |
| 474 | +}); |
0 commit comments