🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Book a DemoInstallSign in
Socket

@transcend-io/type-utils

Package Overview
Dependencies
Maintainers
0
Versions
24
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@transcend-io/type-utils - npm Package Compare versions

Comparing version

to
1.5.0

3

build/codecTools/toJsonSchema.d.ts

@@ -6,6 +6,7 @@ import type { JSONSchema7 } from 'json-schema';

* @param strict - whether to enable strict mode
* @param alwaysIncludeRequired - whether to always include required fields (OpenAI requires this)
* @returns a JSON schema object
* @see https://json-schema.org/understanding-json-schema/basics.html
*/
export declare const toJsonSchema: (_type: any, strict?: boolean) => JSONSchema7;
export declare const toJsonSchema: (_type: any, strict?: boolean, alwaysIncludeRequired?: boolean) => JSONSchema7;
//# sourceMappingURL=toJsonSchema.d.ts.map

@@ -8,6 +8,7 @@ "use strict";

* @param strict - whether to enable strict mode
* @param alwaysIncludeRequired - whether to always include required fields (OpenAI requires this)
* @returns a JSON schema object
* @see https://json-schema.org/understanding-json-schema/basics.html
*/
const toJsonSchema = (_type, strict = false) => {
const toJsonSchema = (_type, strict = false, alwaysIncludeRequired = false) => {
const type = _type;

@@ -33,7 +34,23 @@ if (type._tag === 'StringType') {

if (type._tag === 'UnionType') {
return { anyOf: type.types.map((t) => (0, exports.toJsonSchema)(t, strict)) };
return {
anyOf: type.types.map((t) => (0, exports.toJsonSchema)(t, strict, alwaysIncludeRequired)),
};
}
if (type._tag === 'IntersectionType') {
return { allOf: type.types.map((t) => (0, exports.toJsonSchema)(t, strict)) };
if (type._tag === 'IntersectionType' && !alwaysIncludeRequired) {
return {
allOf: type.types.map((t) => (0, exports.toJsonSchema)(t, strict, alwaysIncludeRequired)),
};
}
if (type._tag === 'IntersectionType' && alwaysIncludeRequired) {
const results = type.types.map((t) => (0, exports.toJsonSchema)(t, strict, alwaysIncludeRequired));
if (!results.every((r) => r.type === 'object')) {
throw new Error('InterfaceType must have all children as type=object');
}
return {
type: 'object',
required: results.map((r) => r.required).flat(),
properties: results.reduce((acc, r) => ({ ...acc, ...r.properties }), {}),
...(strict ? { additionalProperties: false } : {}),
};
}
if (type._tag === 'InterfaceType') {

@@ -45,3 +62,3 @@ return {

key,
(0, exports.toJsonSchema)(subtype, strict),
(0, exports.toJsonSchema)(subtype, strict, alwaysIncludeRequired),
])),

@@ -54,3 +71,3 @@ ...(strict ? { additionalProperties: false } : {}),

type: 'object',
additionalProperties: (0, exports.toJsonSchema)(type.codomain, strict),
additionalProperties: (0, exports.toJsonSchema)(type.codomain, strict, alwaysIncludeRequired),
};

@@ -61,6 +78,15 @@ }

type: 'object',
properties: Object.fromEntries(Object.entries(type.props).map(([key, subtype]) => [
key,
(0, exports.toJsonSchema)(subtype, strict),
])),
...(alwaysIncludeRequired ? { required: Object.keys(type.props) } : {}),
properties: Object.fromEntries(Object.entries(type.props).map(([key, subtype]) => {
const result = (0, exports.toJsonSchema)(subtype, strict, alwaysIncludeRequired);
return [
key,
alwaysIncludeRequired && result.type
? {
...result,
type: [result.type, 'null'],
}
: result,
];
})),
...(strict ? { additionalProperties: false } : {}),

@@ -72,3 +98,3 @@ };

type: 'array',
items: (0, exports.toJsonSchema)(type.type, strict),
items: (0, exports.toJsonSchema)(type.type, strict, alwaysIncludeRequired),
};

@@ -79,3 +105,3 @@ }

type: 'array',
items: type.types.map((t) => (0, exports.toJsonSchema)(t, strict)),
items: type.types.map((t) => (0, exports.toJsonSchema)(t, strict, alwaysIncludeRequired)),
};

@@ -88,3 +114,3 @@ }

return {
...(0, exports.toJsonSchema)(type.type, strict),
...(0, exports.toJsonSchema)(type.type, strict, alwaysIncludeRequired),
description: `Predicate: ${type.predicate.name || type.name}`,

@@ -91,0 +117,0 @@ };

@@ -5,3 +5,3 @@ {

"description": "Small package containing useful typescript utilities.",
"version": "1.4.2",
"version": "1.5.0",
"homepage": "https://github.com/transcend-io/type-utils",

@@ -8,0 +8,0 @@ "repository": {

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet