@@ -2,9 +2,9 @@ import '../../../entries/extend';
22import { z } from 'zod' ;
33
44import type { Schema } from '..' ;
5- import { createInputState , createOutputState } from '../../../testing/state' ;
5+ import { createOutputState } from '../../../testing/state' ;
66
7- import { createOptionalSchema , isOptionalSchema } from './optional' ;
7+ import { createOptionalSchema } from './optional' ;
88
99describe ( 'createOptionalSchema' , ( ) => {
1010 it ( 'creates a simple string schema for an optional string' , ( ) => {
@@ -21,256 +21,3 @@ describe('createOptionalSchema', () => {
2121 expect ( result ) . toEqual ( expected ) ;
2222 } ) ;
2323} ) ;
24-
25- describe ( 'isOptionalSchema' , ( ) => {
26- it ( 'returns true for an optional string' , ( ) => {
27- const schema = z . string ( ) . optional ( ) ;
28-
29- const result = isOptionalSchema ( schema , createOutputState ( ) ) ;
30-
31- expect ( result ) . toEqual ( { optional : true } ) ;
32- } ) ;
33-
34- it ( 'returns true for an optional transform' , ( ) => {
35- const schema = z
36- . string ( )
37- . optional ( )
38- . transform ( ( str ) => str ?. length ?? 0 ) ;
39-
40- const result = isOptionalSchema ( schema , createOutputState ( ) ) ;
41-
42- expect ( result ) . toEqual ( { optional : true } ) ;
43- } ) ;
44-
45- it ( 'returns true for a union with an optional' , ( ) => {
46- const schema = z . union ( [ z . string ( ) , z . string ( ) . optional ( ) ] ) ;
47-
48- const result = isOptionalSchema ( schema , createOutputState ( ) ) ;
49-
50- expect ( result ) . toEqual ( { optional : true } ) ;
51- } ) ;
52-
53- it ( 'returns true for an intersection with an optional' , ( ) => {
54- const schema = z . intersection ( z . string ( ) , z . string ( ) . optional ( ) ) ;
55-
56- const result = isOptionalSchema ( schema , createOutputState ( ) ) ;
57-
58- expect ( result ) . toEqual ( { optional : true } ) ;
59- } ) ;
60-
61- it ( 'returns true for a nullable with an optional' , ( ) => {
62- const schema = z . string ( ) . optional ( ) . nullable ( ) ;
63-
64- const result = isOptionalSchema ( schema , createOutputState ( ) ) ;
65-
66- expect ( result ) . toEqual ( { optional : true } ) ;
67- } ) ;
68-
69- it ( 'returns true for an async transform with an optional' , ( ) => {
70- const schema = z
71- . string ( )
72- . optional ( )
73- // eslint-disable-next-line @typescript-eslint/require-await
74- . transform ( async ( ) => 'x' ) ;
75-
76- const result = isOptionalSchema ( schema , createOutputState ( ) ) ;
77-
78- expect ( result ) . toEqual ( { optional : true } ) ;
79- } ) ;
80-
81- it ( 'returns false for an async transform without an optional' , ( ) => {
82- const schema = z
83- . string ( )
84- // eslint-disable-next-line @typescript-eslint/require-await
85- . transform ( async ( ) => 'x' ) ;
86-
87- const result = isOptionalSchema ( schema , createOutputState ( ) ) ;
88-
89- expect ( result ) . toEqual ( { optional : false } ) ;
90- } ) ;
91-
92- it ( 'returns false for a custom without an optional' , ( ) => {
93- const schema = z . custom < Date > ( ( d ) => d instanceof Date ) ;
94- const result = isOptionalSchema ( schema , createOutputState ( ) ) ;
95-
96- expect ( result ) . toEqual ( { optional : false } ) ;
97- } ) ;
98-
99- it ( 'returns true for a custom undefined without a check' , ( ) => {
100- const schema = z . custom < undefined > ( ) ;
101- const result = isOptionalSchema ( schema , createOutputState ( ) ) ;
102-
103- expect ( result ) . toEqual ( { optional : true } ) ;
104- } ) ;
105-
106- it ( 'returns false for a custom string | undefined without a check' , ( ) => {
107- const schema = z . custom < string | undefined > ( ) ;
108- const result = isOptionalSchema ( schema , createOutputState ( ) ) ;
109-
110- expect ( result ) . toEqual ( { optional : true } ) ;
111- } ) ;
112-
113- it ( 'returns false for a custom string | undefined with a check that returns false for undefined' , ( ) => {
114- const schema = z . custom < string | undefined > (
115- ( toCheck : string | undefined ) => toCheck !== undefined ,
116- ) ;
117- const result = isOptionalSchema ( schema , createOutputState ( ) ) ;
118-
119- expect ( result ) . toEqual ( { optional : false } ) ;
120- } ) ;
121-
122- it ( 'returns true for a custom string | undefined with a check that returns true for undefined' , ( ) => {
123- const schema = z . custom < string | undefined > (
124- ( toCheck : string | undefined ) => toCheck === undefined ,
125- ) ;
126- const result = isOptionalSchema ( schema , createOutputState ( ) ) ;
127-
128- expect ( result ) . toEqual ( { optional : true } ) ;
129- } ) ;
130-
131- it ( 'returns true for a custom undefined with a check' , ( ) => {
132- const schema = z . custom < undefined > ( ( _ : unknown ) => false ) ;
133- const result = isOptionalSchema ( schema , createOutputState ( ) ) ;
134-
135- expect ( result ) . toEqual ( { optional : false } ) ;
136- } ) ;
137-
138- it ( 'returns true for a custom with Date with an optional' , ( ) => {
139- const schema = z . custom < Date > ( ( d ) => d instanceof Date ) . optional ( ) ;
140- const result = isOptionalSchema ( schema , createOutputState ( ) ) ;
141-
142- expect ( result ) . toEqual ( { optional : true } ) ;
143- } ) ;
144-
145- it ( 'returns false for a custom without an optional with async transform' , ( ) => {
146- const schema = z
147- . custom < Date > ( ( d ) => d instanceof Date )
148- // eslint-disable-next-line @typescript-eslint/require-await
149- . transform ( async ( ) => 'x' ) ;
150- const result = isOptionalSchema ( schema , createOutputState ( ) ) ;
151-
152- expect ( result ) . toEqual ( { optional : false } ) ;
153- } ) ;
154-
155- it ( 'returns true for a custom with an optional using a transform' , ( ) => {
156- const schema = z
157- . custom < Date > ( ( d ) => d instanceof Date )
158- . optional ( )
159- // eslint-disable-next-line @typescript-eslint/require-await
160- . transform ( async ( ) => 'x' ) ;
161- const result = isOptionalSchema ( schema , createOutputState ( ) ) ;
162-
163- expect ( result ) . toEqual ( { optional : true } ) ;
164- } ) ;
165-
166- it ( 'returns true for an output state on pipeline' , ( ) => {
167- const schema = z
168- . string ( )
169- . transform ( ( str ) => str . length )
170- . pipe ( z . number ( ) . optional ( ) ) ;
171-
172- const result = isOptionalSchema ( schema , createOutputState ( ) ) ;
173-
174- expect ( result ) . toEqual ( { optional : true } ) ;
175- } ) ;
176-
177- it ( 'returns true for an input state on pipeline' , ( ) => {
178- const schema = z
179- . string ( )
180- . optional ( )
181- . transform ( ( str ) => str ?. length ?? 0 )
182- . pipe ( z . number ( ) ) ;
183-
184- const result = isOptionalSchema ( schema , createInputState ( ) ) ;
185-
186- expect ( result ) . toEqual ( { optional : true } ) ;
187- } ) ;
188-
189- it ( 'returns true for a zod default' , ( ) => {
190- const schema = z . string ( ) . default ( 'a' ) ;
191-
192- const result = isOptionalSchema ( schema , createInputState ( ) ) ;
193-
194- expect ( result ) . toEqual ( {
195- optional : true ,
196- effects : [
197- {
198- type : 'schema' ,
199- creationType : 'input' ,
200- zodType : schema ,
201- path : [ ] ,
202- } ,
203- ] ,
204- } ) ;
205- } ) ;
206-
207- it ( 'returns true for a zod default in output state with effectType input' , ( ) => {
208- const schema = z . string ( ) . default ( 'a' ) . openapi ( { effectType : 'input' } ) ;
209-
210- const result = isOptionalSchema ( schema , createOutputState ( ) ) ;
211-
212- expect ( result ) . toEqual ( {
213- optional : true ,
214- } ) ;
215- } ) ;
216-
217- it ( 'returns false for a zod default in input state with effectType output' , ( ) => {
218- const schema = z . string ( ) . default ( 'a' ) . openapi ( { effectType : 'output' } ) ;
219-
220- const result = isOptionalSchema ( schema , createInputState ( ) ) ;
221-
222- expect ( result ) . toEqual ( {
223- optional : false ,
224- } ) ;
225- } ) ;
226-
227- it ( 'returns true for an input effectType on an output state pipeline' , ( ) => {
228- const schema = z
229- . string ( )
230- . optional ( )
231- . transform ( ( str ) => str ?. length ?? 0 )
232- . pipe ( z . number ( ) )
233- . openapi ( { effectType : 'input' } ) ;
234-
235- const result = isOptionalSchema ( schema , createOutputState ( ) ) ;
236-
237- expect ( result ) . toEqual ( { optional : true } ) ;
238- } ) ;
239-
240- it ( 'returns false for an output effectType on an input state pipeline' , ( ) => {
241- const schema = z
242- . string ( )
243- . optional ( )
244- . transform ( ( str ) => str ?. length ?? 0 )
245- . pipe ( z . number ( ) )
246- . openapi ( { effectType : 'output' } ) ;
247-
248- const result = isOptionalSchema ( schema , createInputState ( ) ) ;
249-
250- expect ( result ) . toEqual ( { optional : false } ) ;
251- } ) ;
252-
253- it ( 'returns true for zod undefined' , ( ) => {
254- const schema = z . undefined ( ) ;
255-
256- const result = isOptionalSchema ( schema , createOutputState ( ) ) ;
257-
258- expect ( result ) . toEqual ( { optional : true } ) ;
259- } ) ;
260-
261- it ( 'returns true for zod never' , ( ) => {
262- const schema = z . never ( ) ;
263-
264- const result = isOptionalSchema ( schema , createOutputState ( ) ) ;
265-
266- expect ( result ) . toEqual ( { optional : true } ) ;
267- } ) ;
268-
269- it ( 'returns true for zod literal undefined' , ( ) => {
270- const schema = z . literal ( undefined ) ;
271-
272- const result = isOptionalSchema ( schema , createOutputState ( ) ) ;
273-
274- expect ( result ) . toEqual ( { optional : true } ) ;
275- } ) ;
276- } ) ;
0 commit comments