Skip to content

Commit 5267588

Browse files
committed
add spare isStandardJSONSchema runtime utils
1 parent ba3b2cd commit 5267588

1 file changed

Lines changed: 19 additions & 3 deletions

File tree

src/utils/neuro_client.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import type { RCEContext } from '@ctx/rce';
1111

1212
import type { NeuroClient } from 'neuro-game-sdk';
1313
import type { reregisterAllActions, registerAction, unregisterAction } from '@/rce';
14-
import type { JSONSchema7Object } from 'json-schema';
14+
import type { JSONSchema7, JSONSchema7Object } from 'json-schema';
1515
import type { StandardJSONSchemaV1 } from '@standard-schema/spec';
1616

1717
//#region Action force utils
@@ -79,7 +79,7 @@ type StandardSchemaInput<TSchema extends StandardJSONSchemaV1 | undefined> =
7979
* ```
8080
*/
8181
// eslint-disable-next-line @typescript-eslint/no-explicit-any
82-
export interface StandardRCEAction<TSchema extends StandardJSONSchemaV1 | undefined = any, K = any>
82+
export interface StandardRCEAction<TSchema extends StandardJSONSchemaV1 | undefined = StandardJSONSchemaV1, K = any>
8383
// eslint-disable-next-line @typescript-eslint/no-explicit-any
8484
extends Omit<RCEAction<StandardSchemaInput<TSchema> & Record<string, any>, K>, 'schema'> {
8585
/**
@@ -476,7 +476,7 @@ export function actionResultEnumFailure<T>(parameterName: string, validValues: T
476476
* This should explain, if possible, why the action failed.
477477
* If omitted, will just return "Action failed.".
478478
* @returns A context message with the specified message.
479-
* @deprecated Action handlers can now be async, and RCE will handle it properly.
479+
* @deprecated Action handlers can now be async, and RCE will handle it properly.
480480
*/
481481
export function contextFailure(message?: string, tag: OutputTag = 'WARNING'): string {
482482
const result = message !== undefined ? `Action failed: ${message}` : 'Action failed.';
@@ -485,3 +485,19 @@ export function contextFailure(message?: string, tag: OutputTag = 'WARNING'): st
485485
}
486486

487487
//#endregion
488+
489+
/**
490+
* Checks if the schema is a Standard JSON Schema or regular JSON Schema.
491+
* @param schema The schema in question.
492+
* @returns A boolean for whether or not it is a Standard JSON Schema or normal JSON Schema.
493+
* @throws If the schema passed is a Standard Schema, but doesn't support Standard JSON Schema.
494+
*/
495+
export function isStandardJSONSchema<T extends JSONSchema7 | StandardJSONSchemaV1>(schema: T): boolean {
496+
if ('~standard' in schema) {
497+
if ('jsonSchema' in schema['~standard']) {
498+
return true;
499+
} else {
500+
throw new Error('Schema used is a Standard Schema, but does not support Standard JSON Schema!');
501+
}
502+
} else return false;
503+
}

0 commit comments

Comments
 (0)