@@ -48,9 +48,11 @@ function mergeGenerated<T>(generated: unknown, factoryValues: unknown): T {
4848 ...factoryValues ,
4949 } as T ;
5050 }
51- return ( factoryValues === undefined || ( isRecord ( factoryValues ) && Object . keys ( factoryValues ) . length === 0 )
52- ? generated
53- : factoryValues ) as T ;
51+ return (
52+ factoryValues === undefined || ( isRecord ( factoryValues ) && Object . keys ( factoryValues ) . length === 0 )
53+ ? generated
54+ : factoryValues
55+ ) as T ;
5456}
5557
5658class JsonSchemaGenerator {
@@ -131,13 +133,17 @@ class JsonSchemaGenerator {
131133 let max = schema . maximum ?? ( integer ? NUMBER_CONSTRAINTS . DEFAULT_INT_MAX : NUMBER_CONSTRAINTS . DEFAULT_MAX ) ;
132134
133135 if ( typeof schema . exclusiveMinimum === 'number' ) {
134- min = integer ? Math . floor ( schema . exclusiveMinimum ) + 1 : schema . exclusiveMinimum + NUMBER_CONSTRAINTS . PRECISION_OFFSET ;
136+ min = integer
137+ ? Math . floor ( schema . exclusiveMinimum ) + 1
138+ : schema . exclusiveMinimum + NUMBER_CONSTRAINTS . PRECISION_OFFSET ;
135139 } else if ( schema . exclusiveMinimum === true ) {
136140 min = integer ? min + 1 : min + NUMBER_CONSTRAINTS . PRECISION_OFFSET ;
137141 }
138142
139143 if ( typeof schema . exclusiveMaximum === 'number' ) {
140- max = integer ? Math . ceil ( schema . exclusiveMaximum ) - 1 : schema . exclusiveMaximum - NUMBER_CONSTRAINTS . PRECISION_OFFSET ;
144+ max = integer
145+ ? Math . ceil ( schema . exclusiveMaximum ) - 1
146+ : schema . exclusiveMaximum - NUMBER_CONSTRAINTS . PRECISION_OFFSET ;
141147 } else if ( schema . exclusiveMaximum === true ) {
142148 max = integer ? max - 1 : max - NUMBER_CONSTRAINTS . PRECISION_OFFSET ;
143149 }
@@ -166,7 +172,11 @@ class JsonSchemaGenerator {
166172 const max = schema . maxLength ?? Math . max ( min , STRING_LENGTHS . DEFAULT ) ;
167173
168174 const formatted = this . #generateFormattedString( schema . format ) ;
169- if ( formatted && formatted . length >= min && ( schema . maxLength === undefined || formatted . length <= schema . maxLength ) ) {
175+ if (
176+ formatted &&
177+ formatted . length >= min &&
178+ ( schema . maxLength === undefined || formatted . length <= schema . maxLength )
179+ ) {
170180 return formatted ;
171181 }
172182
@@ -209,7 +219,9 @@ class JsonSchemaGenerator {
209219 '0000' ,
210220 ] . filter ( ( candidate ) : candidate is string => typeof candidate === 'string' ) ;
211221
212- return candidates . find ( ( candidate ) => candidate . length >= minLength && candidate . length <= maxLength && regex . test ( candidate ) ) ;
222+ return candidates . find (
223+ ( candidate ) => candidate . length >= minLength && candidate . length <= maxLength && regex . test ( candidate ) ,
224+ ) ;
213225 }
214226
215227 #buildPrefixedDigitPattern( pattern : string ) : string | undefined {
@@ -282,7 +294,7 @@ export class JsonSchemaFactory<
282294 declare buildAsync : ( kwargs ?: Partial < T > , options ?: Partial < O > ) => Promise < T > ;
283295
284296 constructor ( schema : JsonSchema , optionsOrFactory ?: O | PartialFactoryFunction < T > , options ?: O ) {
285- let generator : JsonSchemaGenerator ;
297+ const generatorRef : { value ?: JsonSchemaGenerator } = { } ;
286298 const factoryFunction = isFunction ( optionsOrFactory )
287299 ? optionsOrFactory
288300 : ( ( ( ) => ( { } ) ) as PartialFactoryFunction < T > ) ;
@@ -292,17 +304,24 @@ export class JsonSchemaFactory<
292304 ? ( optionsOrFactory as O )
293305 : ( { } as O ) ;
294306
295- super ( ( ( factory , iteration , kwargs ) => {
296- const generated = generator . generate ( schema ) ;
297- const factoryValues = ( factoryFunction as FactoryFunction < T > ) ( factory as Factory < T > , iteration , kwargs ) ;
298- if ( factoryValues instanceof Promise ) {
299- return factoryValues . then ( ( values ) => mergeGenerated < T > ( generated , values ) ) as Promise < T > ;
300- }
301- return mergeGenerated < T > ( generated , factoryValues ) ;
302- } ) as FactoryFunction < T > , factoryOptions ) ;
307+ super (
308+ ( ( factory , iteration , kwargs ) => {
309+ const generator = generatorRef . value ;
310+ if ( ! generator ) {
311+ throw new ConfigurationError ( 'JSON Schema generator has not been initialized' ) ;
312+ }
313+ const generated = generator . generate ( schema ) ;
314+ const factoryValues = ( factoryFunction as FactoryFunction < T > ) ( factory as Factory < T > , iteration , kwargs ) ;
315+ if ( factoryValues instanceof Promise ) {
316+ return factoryValues . then ( ( values ) => mergeGenerated < T > ( generated , values ) ) as Promise < T > ;
317+ }
318+ return mergeGenerated < T > ( generated , factoryValues ) ;
319+ } ) as FactoryFunction < T > ,
320+ factoryOptions ,
321+ ) ;
303322
304323 this . #schema = schema ;
305- generator = new JsonSchemaGenerator ( this as unknown as Factory < unknown > , schema ) ;
324+ generatorRef . value = new JsonSchemaGenerator ( this as unknown as Factory < unknown > , schema ) ;
306325
307326 const ajv = new Ajv ( {
308327 allErrors : true ,
@@ -328,7 +347,9 @@ export class JsonSchemaFactory<
328347
329348 #validate( value : T ) : T {
330349 if ( ! this . #validator( value ) ) {
331- throw new ValidationError ( `Generated value does not match JSON Schema: ${ JSON . stringify ( this . #validator. errors ) } ` ) ;
350+ throw new ValidationError (
351+ `Generated value does not match JSON Schema: ${ JSON . stringify ( this . #validator. errors ) } ` ,
352+ ) ;
332353 }
333354 return value ;
334355 }
0 commit comments