@amritk/adapters 0.2.6 → 0.2.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,9 +4,10 @@ import type { JSONSchema } from 'json-schema-typed/draft-2020-12';
4
4
  * `@valibot/to-json-schema`.
5
5
  *
6
6
  * Valibot's `date` schema has no JSON Schema representation and would otherwise
7
- * throw, so we run with `errorMode: 'ignore'` (other unsupported constructs
8
- * degrade to an open schema rather than failing the whole conversion) and use
9
- * the `overrideSchema` hook to rewrite dates into the shared `x-mjst` instanceOf
7
+ * throw, so we run with `errorMode: 'warn'` (other unsupported constructs degrade
8
+ * to an open schema rather than failing the whole conversion, and the converter
9
+ * logs which ones — so the widening is visible instead of silent) and use the
10
+ * `overrideSchema` hook to rewrite dates into the shared `x-mjst` instanceOf
10
11
  * extension — the same handling TypeBox and Zod dates receive.
11
12
  */
12
13
  export declare const valibotToJsonSchema: (source: unknown) => Promise<JSONSchema>;
@@ -1 +1 @@
1
- {"version":3,"file":"valibot-to-json-schema.d.ts","sourceRoot":"","sources":["../src/valibot-to-json-schema.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,iCAAiC,CAAA;AAuCjE;;;;;;;;;GASG;AACH,eAAO,MAAM,mBAAmB,WAAkB,OAAO,KAAG,OAAO,CAAC,UAAU,CA0B7E,CAAA"}
1
+ {"version":3,"file":"valibot-to-json-schema.d.ts","sourceRoot":"","sources":["../src/valibot-to-json-schema.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,iCAAiC,CAAA;AAuCjE;;;;;;;;;;GAUG;AACH,eAAO,MAAM,mBAAmB,WAAkB,OAAO,KAAG,OAAO,CAAC,UAAU,CA0B7E,CAAA"}
@@ -24,9 +24,10 @@ const loadToJsonSchema = async () => {
24
24
  * `@valibot/to-json-schema`.
25
25
  *
26
26
  * Valibot's `date` schema has no JSON Schema representation and would otherwise
27
- * throw, so we run with `errorMode: 'ignore'` (other unsupported constructs
28
- * degrade to an open schema rather than failing the whole conversion) and use
29
- * the `overrideSchema` hook to rewrite dates into the shared `x-mjst` instanceOf
27
+ * throw, so we run with `errorMode: 'warn'` (other unsupported constructs degrade
28
+ * to an open schema rather than failing the whole conversion, and the converter
29
+ * logs which ones — so the widening is visible instead of silent) and use the
30
+ * `overrideSchema` hook to rewrite dates into the shared `x-mjst` instanceOf
30
31
  * extension — the same handling TypeBox and Zod dates receive.
31
32
  */
32
33
  export const valibotToJsonSchema = async (source) => {
@@ -38,7 +39,7 @@ export const valibotToJsonSchema = async (source) => {
38
39
  let json;
39
40
  try {
40
41
  json = toJsonSchema(source, {
41
- errorMode: 'ignore',
42
+ errorMode: 'warn',
42
43
  overrideSchema: (ctx) => {
43
44
  if (ctx.valibotSchema?.type === 'date')
44
45
  return { [MJST_EXTENSION_KEY]: { instanceOf: 'Date' } };
@@ -1 +1 @@
1
- {"version":3,"file":"zod-to-json-schema.d.ts","sourceRoot":"","sources":["../src/zod-to-json-schema.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,iCAAiC,CAAA;AA4CjE;;;;;;;;;GASG;AACH,eAAO,MAAM,eAAe,WAAkB,OAAO,KAAG,OAAO,CAAC,UAAU,CA+BzE,CAAA"}
1
+ {"version":3,"file":"zod-to-json-schema.d.ts","sourceRoot":"","sources":["../src/zod-to-json-schema.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,iCAAiC,CAAA;AA4CjE;;;;;;;;;GASG;AACH,eAAO,MAAM,eAAe,WAAkB,OAAO,KAAG,OAAO,CAAC,UAAU,CA6CzE,CAAA"}
@@ -38,6 +38,11 @@ export const zodToJsonSchema = async (source) => {
38
38
  throw new Error(`Zod adapter expected a Zod schema but received ${received}.`);
39
39
  }
40
40
  const toJSONSchema = await loadToJsonSchema();
41
+ // Zod types with no JSON Schema equivalent that we do *not* rescue into an
42
+ // `x-mjst` hint. `unrepresentable: 'any'` turns these into `{}` (accepts
43
+ // anything), which silently widens the generated type — so we surface them.
44
+ const LOSSY_TYPES = new Set(['symbol', 'nan', 'void', 'undefined', 'never', 'map', 'set', 'promise', 'function']);
45
+ const droppedTypes = new Set();
41
46
  let json;
42
47
  try {
43
48
  json = toJSONSchema(source, {
@@ -54,12 +59,18 @@ export const zodToJsonSchema = async (source) => {
54
59
  delete ctx.jsonSchema[key];
55
60
  ctx.jsonSchema[MJST_EXTENSION_KEY] = { primitive: 'bigint' };
56
61
  }
62
+ else if (type && LOSSY_TYPES.has(type)) {
63
+ droppedTypes.add(type);
64
+ }
57
65
  },
58
66
  });
59
67
  }
60
68
  catch (error) {
61
69
  throw new Error(`Zod adapter failed to convert the schema. Is it a valid Zod schema?\n${String(error)}`);
62
70
  }
71
+ if (droppedTypes.size > 0) {
72
+ console.warn(`[mjst] Zod adapter: ${[...droppedTypes].sort().join(', ')} ${droppedTypes.size === 1 ? 'has' : 'have'} no JSON Schema representation and became "accept anything". The generated type will be wider than the Zod schema.`);
73
+ }
63
74
  // The dialect marker is noise for the generators, which already target 2020-12.
64
75
  delete json['$schema'];
65
76
  return json;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@amritk/adapters",
3
- "version": "0.2.6",
3
+ "version": "0.2.8",
4
4
  "description": "Convert schemas from external libraries (TypeBox, Zod, ...) into JSON Schema for mjst.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -38,7 +38,7 @@
38
38
  },
39
39
  "dependencies": {
40
40
  "json-schema-typed": "^8.0.1",
41
- "@amritk/helpers": "0.7.0"
41
+ "@amritk/helpers": "0.8.0"
42
42
  },
43
43
  "exports": {
44
44
  "./package.json": "./package.json",
@@ -43,9 +43,10 @@ const loadToJsonSchema = async (): Promise<ToJsonSchema> => {
43
43
  * `@valibot/to-json-schema`.
44
44
  *
45
45
  * Valibot's `date` schema has no JSON Schema representation and would otherwise
46
- * throw, so we run with `errorMode: 'ignore'` (other unsupported constructs
47
- * degrade to an open schema rather than failing the whole conversion) and use
48
- * the `overrideSchema` hook to rewrite dates into the shared `x-mjst` instanceOf
46
+ * throw, so we run with `errorMode: 'warn'` (other unsupported constructs degrade
47
+ * to an open schema rather than failing the whole conversion, and the converter
48
+ * logs which ones — so the widening is visible instead of silent) and use the
49
+ * `overrideSchema` hook to rewrite dates into the shared `x-mjst` instanceOf
49
50
  * extension — the same handling TypeBox and Zod dates receive.
50
51
  */
51
52
  export const valibotToJsonSchema = async (source: unknown): Promise<JSONSchema> => {
@@ -59,7 +60,7 @@ export const valibotToJsonSchema = async (source: unknown): Promise<JSONSchema>
59
60
  let json: Record<string, unknown>
60
61
  try {
61
62
  json = toJsonSchema(source, {
62
- errorMode: 'ignore',
63
+ errorMode: 'warn',
63
64
  overrideSchema: (ctx) => {
64
65
  if (ctx.valibotSchema?.type === 'date') return { [MJST_EXTENSION_KEY]: { instanceOf: 'Date' } }
65
66
  if (ctx.valibotSchema?.type === 'bigint') return { [MJST_EXTENSION_KEY]: { primitive: 'bigint' } }
@@ -1,5 +1,5 @@
1
1
  import { generateTypeDefinition } from '@amritk/helpers/generate-type-definition'
2
- import { describe, expect, it } from 'vitest'
2
+ import { describe, expect, it, vi } from 'vitest'
3
3
  import * as zodModule from 'zod'
4
4
 
5
5
  import { zodToJsonSchema } from './zod-to-json-schema'
@@ -37,6 +37,16 @@ describe('zodToJsonSchema', () => {
37
37
  expect(result).not.toHaveProperty('$schema')
38
38
  })
39
39
 
40
+ it('warns when a lossy (unrepresentable) Zod type is widened to "accept anything"', async () => {
41
+ const warn = vi.spyOn(console, 'warn').mockImplementation(() => {})
42
+ try {
43
+ await zodToJsonSchema(z.object({ id: z.string(), sym: z.symbol() }))
44
+ expect(warn).toHaveBeenCalledWith(expect.stringMatching(/Zod adapter: symbol/))
45
+ } finally {
46
+ warn.mockRestore()
47
+ }
48
+ })
49
+
40
50
  it('converts enums to a string schema with an enum list', async () => {
41
51
  const result = await zodToJsonSchema(z.object({ role: z.enum(['admin', 'user']) }))
42
52
 
@@ -61,6 +61,12 @@ export const zodToJsonSchema = async (source: unknown): Promise<JSONSchema> => {
61
61
 
62
62
  const toJSONSchema = await loadToJsonSchema()
63
63
 
64
+ // Zod types with no JSON Schema equivalent that we do *not* rescue into an
65
+ // `x-mjst` hint. `unrepresentable: 'any'` turns these into `{}` (accepts
66
+ // anything), which silently widens the generated type — so we surface them.
67
+ const LOSSY_TYPES = new Set(['symbol', 'nan', 'void', 'undefined', 'never', 'map', 'set', 'promise', 'function'])
68
+ const droppedTypes = new Set<string>()
69
+
64
70
  let json: Record<string, unknown>
65
71
  try {
66
72
  json = toJSONSchema(source, {
@@ -73,6 +79,8 @@ export const zodToJsonSchema = async (source: unknown): Promise<JSONSchema> => {
73
79
  } else if (type === 'bigint') {
74
80
  for (const key of Object.keys(ctx.jsonSchema)) delete ctx.jsonSchema[key]
75
81
  ctx.jsonSchema[MJST_EXTENSION_KEY] = { primitive: 'bigint' }
82
+ } else if (type && LOSSY_TYPES.has(type)) {
83
+ droppedTypes.add(type)
76
84
  }
77
85
  },
78
86
  })
@@ -80,6 +88,12 @@ export const zodToJsonSchema = async (source: unknown): Promise<JSONSchema> => {
80
88
  throw new Error(`Zod adapter failed to convert the schema. Is it a valid Zod schema?\n${String(error)}`)
81
89
  }
82
90
 
91
+ if (droppedTypes.size > 0) {
92
+ console.warn(
93
+ `[mjst] Zod adapter: ${[...droppedTypes].sort().join(', ')} ${droppedTypes.size === 1 ? 'has' : 'have'} no JSON Schema representation and became "accept anything". The generated type will be wider than the Zod schema.`,
94
+ )
95
+ }
96
+
83
97
  // The dialect marker is noise for the generators, which already target 2020-12.
84
98
  delete json['$schema']
85
99