Skip to content

Commit 8445a90

Browse files
committed
Add zod override to remove redundant minimum / maximum
1 parent ad439d9 commit 8445a90

1 file changed

Lines changed: 29 additions & 2 deletions

File tree

src/utils/neuro_client.ts

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import type { NeuroClient } from 'neuro-game-sdk';
1313
import type { reregisterAllActions, registerAction, unregisterAction } from '@/rce';
1414
import type { JSONSchema7 } from 'json-schema';
1515
import type { StandardJSONSchemaV1 } from '@standard-schema/spec';
16+
import z from 'zod';
1617

1718
//#region Action force utils
1819

@@ -481,16 +482,42 @@ export type SupportedSchemaDrafts = 'draft-07' | 'draft-2020-12';
481482
export function attemptConvertStandardJSONSchema(schema: StandardJSONSchemaV1): { schema: JSONSchema7, type: SupportedSchemaDrafts } {
482483
let jsonSchema: JSONSchema7;
483484
let type: SupportedSchemaDrafts;
485+
484486
try {
485487
type = 'draft-07';
486-
jsonSchema = schema['~standard'].jsonSchema.input({ target: type });
488+
if (schema['~standard'].vendor === 'zod') {
489+
jsonSchema = z.toJSONSchema(schema as z.ZodType, {
490+
target: type,
491+
override: (ctx) => zodSchemaOverride(ctx.jsonSchema),
492+
}) as JSONSchema7;
493+
} else {
494+
jsonSchema = schema['~standard'].jsonSchema.input({ target: type });
495+
}
487496
} catch {
488497
type = 'draft-2020-12';
489-
jsonSchema = schema['~standard'].jsonSchema.input({ target: type });
498+
if (schema['~standard'].vendor === 'zod') {
499+
jsonSchema = z.toJSONSchema(schema as z.ZodType, {
500+
target: type,
501+
override: (ctx) => zodSchemaOverride(ctx.jsonSchema),
502+
}) as JSONSchema7;
503+
} else {
504+
jsonSchema = schema['~standard'].jsonSchema.input({ target: type });
505+
}
490506
}
491507
delete jsonSchema['$schema'];
508+
492509
return {
493510
schema: jsonSchema,
494511
type,
495512
};
496513
}
514+
515+
function zodSchemaOverride(jsonSchema: z.core.JSONSchema.JSONSchema): void {
516+
if (jsonSchema.type === 'integer') {
517+
// Delete redundant minima / maxima
518+
if (jsonSchema.maximum === Number.MAX_SAFE_INTEGER)
519+
delete jsonSchema.maximum;
520+
if (jsonSchema.minimum === Number.MIN_SAFE_INTEGER)
521+
delete jsonSchema.minimum;
522+
}
523+
}

0 commit comments

Comments
 (0)