Skip to content

Commit e193c4d

Browse files
committed
feat(extract-json-schema): Added lookup from schema object to its top-level ref name
1 parent e03ccb3 commit e193c4d

File tree

3 files changed

+31
-5
lines changed

3 files changed

+31
-5
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ or as all types at once, into one big JSON schema. In this case, all validation
318318
```ts
319319
import { extractJsonSchema } from "suretype"
320320

321-
const { schema: jsonSchema, lookup } =
321+
const { schema: jsonSchema, lookup, schemaRefName } =
322322
extractJsonSchema( [ userSchema, messageSchema ], { /* opts... */ } );
323323
```
324324

@@ -361,11 +361,14 @@ The result is an object on the form:
361361
interface ExtractedJsonSchema {
362362
schema: SchemaWithDefinitions; // Contains a 'definitions' property
363363
lookup: Map< CoreValidator< unknown >, any >;
364+
schemaRefName: Map< any, string >;
364365
}
365366
```
366367

367368
The `lookup` is useful to lookup the json schema for a certain validator object reference, especially unnamed ones which are not included in the schema.
368369

370+
The `schemaRefName` contains a lookup map from (top-level) schema object to its name as used when referring to it, not necessarily the same as what it is internally named, if there where naming conflicts and `OnTopLevelNameConflict` is `rename`.
371+
369372
In the example above, the `jsonSchema` *object* (which can be `JSON.stringify`'d) will be something like:
370373

371374
<details style="padding-left: 32px;border-left: 4px solid gray;">

lib/extract-json-schema.test.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -265,16 +265,17 @@ describe( "extract-json-schema", ( ) =>
265265
const schema2 = v.anyOf( [ v.string( ), v.boolean( ) ] );
266266
const schema3 = v.anyOf( [ v.string( ), schema1 ] );
267267

268-
const { schema, lookup } = extractJsonSchema(
268+
const { schema, lookup, schemaRefName } = extractJsonSchema(
269269
[ schema1, schema2, schema3 ],
270270
{ onNonSuretypeValidator: 'lookup' }
271271
);
272272

273-
schema;
274-
lookup;
275273
expect( schema ).toMatchSnapshot( );
276274
expect( lookup.get( schema1 ) ).toMatchSnapshot( );
277275
expect( lookup.get( schema2 ) ).toMatchSnapshot( );
278276
expect( lookup.get( schema3 ) ).toMatchSnapshot( );
277+
expect( schemaRefName.get( lookup.get( schema1 ) ) ).toBe( 'Foo' );
278+
expect( schemaRefName.get( lookup.get( schema2 ) ) ).toBeUndefined( );
279+
expect( schemaRefName.get( lookup.get( schema3 ) ) ).toBeUndefined( );
279280
} );
280281
} );

lib/extract-json-schema.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,22 @@ export interface SchemaWithDefinitions
2525

2626
export interface ExtractedJsonSchema
2727
{
28+
/**
29+
* The extracted schema definitions
30+
*/
2831
schema: SchemaWithDefinitions;
32+
33+
/**
34+
* Lookup from validator to schema object
35+
*/
2936
lookup: Map< CoreValidator< unknown >, any >;
37+
38+
/**
39+
* Lookup from top-level schema object to its corresponding name.
40+
* This is its referrable name, which might not be the same as its inner
41+
* name if it had to be renamed due to top-level naming conflicts.
42+
*/
43+
schemaRefName: Map< any, string >;
3044
}
3145

3246
/**
@@ -86,7 +100,15 @@ export function extractJsonSchema(
86100
);
87101
const { schema, lookup } = traverser.getSchema( );
88102

89-
return { schema, lookup };
103+
const schemaRefName = new Map< any, string >( );
104+
Object
105+
.entries( ( schema as SchemaWithDefinitions ).definitions )
106+
.forEach( ( [ name, schema ] ) =>
107+
{
108+
schemaRefName.set( schema, name );
109+
} );
110+
111+
return { schema, lookup, schemaRefName };
90112
}
91113

92114
export type ExtractSingleSchemaResult =

0 commit comments

Comments
 (0)