@power-plant/schema 0.0.25 → 0.0.26
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.
- package/dist/codegen.cjs +304 -11
- package/dist/codegen.mjs +300 -11
- package/dist/constants-B7xonHLD.cjs +128 -0
- package/dist/constants-COGt3eeW.mjs +87 -0
- package/dist/constants-COGt3eeW.mjs.map +1 -0
- package/dist/helpers-reqtcaa8.mjs +921 -0
- package/dist/helpers-reqtcaa8.mjs.map +1 -0
- package/dist/helpers-sXsSapgI.cjs +1269 -0
- package/dist/index.cjs +788 -2
- package/dist/index.mjs +706 -2
- package/dist/rolldown-runtime-C_NdSu1c.cjs +34 -0
- package/dist/storage/index.cjs +6 -1
- package/dist/storage/index.mjs +3 -1
- package/dist/storage-B8aDmNpv.cjs +934 -0
- package/dist/storage-C3BT2Xh-.mjs +917 -0
- package/dist/storage-C3BT2Xh-.mjs.map +1 -0
- package/dist/valibot.cjs +124 -1
- package/dist/valibot.mjs +119 -1
- package/dist/zod.cjs +150 -1
- package/dist/zod.mjs +143 -1
- package/package.json +4 -4
- package/dist/constants-CjTQhR-l.cjs +0 -1
- package/dist/constants-lk1mMbgx.mjs +0 -2
- package/dist/constants-lk1mMbgx.mjs.map +0 -1
- package/dist/helpers-CFED4EbH.cjs +0 -1
- package/dist/helpers-s2Zqtb-K.mjs +0 -2
- package/dist/helpers-s2Zqtb-K.mjs.map +0 -1
- package/dist/rolldown-runtime-CMqjfN_6.cjs +0 -1
- package/dist/storage-BccFP9xn.mjs +0 -2
- package/dist/storage-BccFP9xn.mjs.map +0 -1
- package/dist/storage-CgfRQlqS.cjs +0 -1
|
@@ -0,0 +1,921 @@
|
|
|
1
|
+
import { a as SCHEMA_ARRAY_CONCAT_KEYWORDS, n as JSON_SCHEMA_PRIMITIVE_TYPES, o as SCHEMA_RECORD_KEYWORDS, r as JSON_SCHEMA_TYPES, s as SCHEMA_SINGLE_KEYWORDS, t as JSON_SCHEMA_METADATA_KEYS } from "./constants-COGt3eeW.mjs";
|
|
2
|
+
import { isSetObject } from "@stryke/type-checks/is-set-object";
|
|
3
|
+
import { isSetString } from "@stryke/type-checks/is-set-string";
|
|
4
|
+
import { isBoolean, isFunction, isSetObject as isSetObject$1, isSetString as isSetString$1, isString } from "@stryke/type-checks";
|
|
5
|
+
import { findFileExtensionSafe } from "@stryke/path/find";
|
|
6
|
+
import { VALID_OBJECT_SOURCE_EXTENSIONS } from "@stryke/resolve/constants";
|
|
7
|
+
import { getUnique } from "@stryke/helpers/get-unique";
|
|
8
|
+
import defu from "defu";
|
|
9
|
+
|
|
10
|
+
//#region src/metadata.ts
|
|
11
|
+
/**
|
|
12
|
+
* Applies Powerlines schema metadata onto a JSON Schema fragment.
|
|
13
|
+
*
|
|
14
|
+
* @param schema - The JSON Schema fragment to apply metadata to.
|
|
15
|
+
* @param metadata - The Powerlines schema metadata to apply.
|
|
16
|
+
* @returns A new JSON Schema fragment with the metadata applied.
|
|
17
|
+
*/
|
|
18
|
+
function applyJsonSchemaMetadata(schema, metadata) {
|
|
19
|
+
if (!metadata || !isSetObject(schema)) return schema;
|
|
20
|
+
const result = { ...schema };
|
|
21
|
+
const mutableResult = result;
|
|
22
|
+
for (const key of JSON_SCHEMA_METADATA_KEYS) {
|
|
23
|
+
const value = metadata[key];
|
|
24
|
+
if (value !== void 0 && value !== null) mutableResult[key] = value;
|
|
25
|
+
}
|
|
26
|
+
return result;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Normalizes the JSON Schema `type` keyword to a string array.
|
|
30
|
+
*
|
|
31
|
+
* @remarks
|
|
32
|
+
* This function ensures that the `type` keyword of a JSON Schema fragment is always represented as an array of strings, even if it was originally defined as a single string. This normalization simplifies type checking and processing of JSON Schemas by providing a consistent format for the `type` information.
|
|
33
|
+
*
|
|
34
|
+
* @param schema - The JSON Schema fragment to read types from.
|
|
35
|
+
* @returns An array of JSON Schema primitive type names defined in the `type` keyword, or an empty array if no valid types are found.
|
|
36
|
+
*/
|
|
37
|
+
function readSchemaTypes(schema) {
|
|
38
|
+
if (!isSetObject(schema)) return [];
|
|
39
|
+
const objectSchema = schema;
|
|
40
|
+
if (Array.isArray(objectSchema.type)) return objectSchema.type.filter((type) => isSetString(type));
|
|
41
|
+
if (isSetString(objectSchema.type) && objectSchema.type !== "object" && objectSchema.type !== "array") return [objectSchema.type];
|
|
42
|
+
return [];
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Returns the primary non-null JSON Schema type name for a fragment.
|
|
46
|
+
*
|
|
47
|
+
* @param schema - The JSON Schema fragment to check.
|
|
48
|
+
* @returns The primary non-null JSON Schema type name, or `undefined` if none is found.
|
|
49
|
+
*/
|
|
50
|
+
function getPrimarySchemaType(schema) {
|
|
51
|
+
if (!isSetObject(schema)) return;
|
|
52
|
+
return readSchemaTypes(schema).find((type) => type !== "null");
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
//#endregion
|
|
56
|
+
//#region src/type-checks.ts
|
|
57
|
+
/**
|
|
58
|
+
* Checks if the provided entry is a file reference.
|
|
59
|
+
*
|
|
60
|
+
* @param entry - The entry to check.
|
|
61
|
+
* @returns True if the entry is a file reference, false otherwise.
|
|
62
|
+
*/
|
|
63
|
+
function isFileReference(entry) {
|
|
64
|
+
return !isString(entry) && entry.file !== void 0;
|
|
65
|
+
}
|
|
66
|
+
const isSetNumber = (value) => typeof value === "number" && Number.isFinite(value);
|
|
67
|
+
const isSchemaLikeValue = (value) => isSetObject$1(value) || isBoolean(value);
|
|
68
|
+
const isRecordOfSchemaLike = (value) => isSetObject$1(value) && Object.values(value).every((item) => isSchemaLikeValue(item));
|
|
69
|
+
const isVocabularyMap = (value) => isSetObject$1(value) && Object.values(value).every((item) => isBoolean(item));
|
|
70
|
+
const isStringArray = (value) => Array.isArray(value) && value.every((item) => isSetString$1(item));
|
|
71
|
+
const isRecordOfStringArrays = (value) => isSetObject$1(value) && Object.values(value).every((item) => isStringArray(item));
|
|
72
|
+
const JSON_SCHEMA_PRIMITIVE_TYPE_SET = new Set(JSON_SCHEMA_PRIMITIVE_TYPES);
|
|
73
|
+
const JSON_SCHEMA_TYPE_SET = new Set(JSON_SCHEMA_TYPES);
|
|
74
|
+
/**
|
|
75
|
+
* A helper type guard to check if a value is a JSON Schema primitive type.
|
|
76
|
+
*
|
|
77
|
+
* @param value - The value to check.
|
|
78
|
+
* @returns True if the value is a JSON Schema primitive type, false otherwise.
|
|
79
|
+
*/
|
|
80
|
+
function isJsonSchemaPrimitiveType(value) {
|
|
81
|
+
return isSetString$1(value) && JSON_SCHEMA_PRIMITIVE_TYPE_SET.has(value);
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* A helper type guard to check if a value is a JSON Schema type.
|
|
85
|
+
*
|
|
86
|
+
* @param value - The value to check.
|
|
87
|
+
* @returns True if the value is a JSON Schema type, false otherwise.
|
|
88
|
+
*/
|
|
89
|
+
function isJsonSchemaType(value) {
|
|
90
|
+
return isSetString$1(value) && JSON_SCHEMA_TYPE_SET.has(value);
|
|
91
|
+
}
|
|
92
|
+
const DATE_FORMAT_SET = /* @__PURE__ */ new Set([
|
|
93
|
+
"date",
|
|
94
|
+
"time",
|
|
95
|
+
"date-time",
|
|
96
|
+
"iso-time",
|
|
97
|
+
"iso-date-time",
|
|
98
|
+
"unix-time"
|
|
99
|
+
]);
|
|
100
|
+
const isSetBigint = (value) => typeof value === "bigint";
|
|
101
|
+
const UNTYPED_TYPE_NAME_SET = /* @__PURE__ */ new Set([
|
|
102
|
+
"string",
|
|
103
|
+
"number",
|
|
104
|
+
"bigint",
|
|
105
|
+
"boolean",
|
|
106
|
+
"symbol",
|
|
107
|
+
"function",
|
|
108
|
+
"object",
|
|
109
|
+
"any",
|
|
110
|
+
"array"
|
|
111
|
+
]);
|
|
112
|
+
const isUntypedJSType = (value) => isSetString$1(value) && UNTYPED_TYPE_NAME_SET.has(value);
|
|
113
|
+
const isUntypedTypeDescriptor = (value) => {
|
|
114
|
+
if (!isSetObject$1(value)) return false;
|
|
115
|
+
const descriptor = value;
|
|
116
|
+
if (descriptor.type !== void 0 && !(isSetString$1(descriptor.type) && isUntypedJSType(descriptor.type) || Array.isArray(descriptor.type) && descriptor.type.every((item) => isUntypedJSType(item)))) return false;
|
|
117
|
+
if (descriptor.tsType !== void 0 && !isSetString$1(descriptor.tsType)) return false;
|
|
118
|
+
if (descriptor.markdownType !== void 0 && !isSetString$1(descriptor.markdownType)) return false;
|
|
119
|
+
if (descriptor.items !== void 0 && !(isUntypedTypeDescriptor(descriptor.items) || Array.isArray(descriptor.items) && descriptor.items.every((item) => isUntypedTypeDescriptor(item)))) return false;
|
|
120
|
+
return true;
|
|
121
|
+
};
|
|
122
|
+
const isUntypedFunctionArg = (value) => {
|
|
123
|
+
if (!isUntypedTypeDescriptor(value)) return false;
|
|
124
|
+
const arg = value;
|
|
125
|
+
if (arg.name !== void 0 && !isSetString$1(arg.name)) return false;
|
|
126
|
+
return arg.optional === void 0 || typeof arg.optional === "boolean";
|
|
127
|
+
};
|
|
128
|
+
const isRecordOfUntypedSchema = (value) => isSetObject$1(value) && Object.values(value).every((item) => isUntypedSchema(item));
|
|
129
|
+
const isArrayOf = (value, predicate) => Array.isArray(value) && value.every((item) => predicate(item));
|
|
130
|
+
const isTupleOfTwo = (value, aPredicate, bPredicate) => Array.isArray(value) && value.length === 2 && aPredicate(value[0]) && bPredicate(value[1]);
|
|
131
|
+
const isOptionalString = (value, allowEmpty = false) => value === void 0 || isString(value) && (allowEmpty || value.length > 0);
|
|
132
|
+
const isOptionalBoolean = (value) => value === void 0 || isBoolean(value);
|
|
133
|
+
const isOptionalNumber = (value) => value === void 0 || isSetNumber(value);
|
|
134
|
+
const isOptionalBigint = (value) => value === void 0 || isSetBigint(value);
|
|
135
|
+
const isOptionalJsonSchema = (value) => value === void 0 || isJsonSchema(value);
|
|
136
|
+
const isOptionalJsonSchemaArray = (value) => value === void 0 || isArrayOf(value, isJsonSchema);
|
|
137
|
+
const isOptionalPrimitiveTypeArray = (value) => value === void 0 || Array.isArray(value) && value.every((item) => isSetString$1(item) && JSON_SCHEMA_PRIMITIVE_TYPE_SET.has(item));
|
|
138
|
+
const isOptionalJsonSchemaTypeArray = (value) => value === void 0 || Array.isArray(value) && value.every((item) => isSetString$1(item) && JSON_SCHEMA_TYPE_SET.has(item));
|
|
139
|
+
const hasValidJsonSchemaKeywords = (schema) => {
|
|
140
|
+
if (!isOptionalString(schema.$id)) return false;
|
|
141
|
+
if (!isOptionalString(schema.$schema)) return false;
|
|
142
|
+
if (schema.$vocabulary !== void 0 && !isVocabularyMap(schema.$vocabulary)) return false;
|
|
143
|
+
if (!isOptionalString(schema.$comment, true)) return false;
|
|
144
|
+
if (!isOptionalString(schema.$anchor)) return false;
|
|
145
|
+
if (schema.$defs !== void 0 && !isRecordOfSchemaLike(schema.$defs)) return false;
|
|
146
|
+
if (!isOptionalString(schema.$dynamicRef)) return false;
|
|
147
|
+
if (!isOptionalString(schema.$dynamicAnchor)) return false;
|
|
148
|
+
if (!isOptionalString(schema.name)) return false;
|
|
149
|
+
if (!isOptionalString(schema.title, true)) return false;
|
|
150
|
+
if (!isOptionalString(schema.description, true)) return false;
|
|
151
|
+
if (!isOptionalString(schema.docs, true)) return false;
|
|
152
|
+
if (schema.examples !== void 0 && !Array.isArray(schema.examples)) return false;
|
|
153
|
+
if (schema.alias !== void 0 && !isStringArray(schema.alias)) return false;
|
|
154
|
+
if (schema.tags !== void 0 && !isStringArray(schema.tags)) return false;
|
|
155
|
+
if (!isOptionalBoolean(schema.deprecated)) return false;
|
|
156
|
+
if (!isOptionalBoolean(schema.hidden)) return false;
|
|
157
|
+
if (!isOptionalBoolean(schema.ignore)) return false;
|
|
158
|
+
if (!isOptionalBoolean(schema.internal)) return false;
|
|
159
|
+
if (!isOptionalBoolean(schema.runtime)) return false;
|
|
160
|
+
if (!isOptionalBoolean(schema.readOnly)) return false;
|
|
161
|
+
if (!isOptionalBoolean(schema.writeOnly)) return false;
|
|
162
|
+
if (!isOptionalJsonSchemaArray(schema.allOf)) return false;
|
|
163
|
+
if (!isOptionalJsonSchemaArray(schema.anyOf)) return false;
|
|
164
|
+
if (!isOptionalJsonSchemaArray(schema.oneOf)) return false;
|
|
165
|
+
if (!isOptionalJsonSchema(schema.not)) return false;
|
|
166
|
+
if (!isOptionalJsonSchema(schema.if)) return false;
|
|
167
|
+
if (!isOptionalJsonSchema(schema.then)) return false;
|
|
168
|
+
if (!isOptionalJsonSchema(schema.else)) return false;
|
|
169
|
+
return true;
|
|
170
|
+
};
|
|
171
|
+
/**
|
|
172
|
+
* Type guard for shared JSON Schema keyword groups.
|
|
173
|
+
*
|
|
174
|
+
* @param input - The value to check.
|
|
175
|
+
* @returns True if the input is a JSON Schema keyword group, false otherwise.
|
|
176
|
+
*/
|
|
177
|
+
function isJsonSchemaKeywords(input) {
|
|
178
|
+
if (!isSetObject$1(input)) return false;
|
|
179
|
+
return hasValidJsonSchemaKeywords(input);
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* Type guard for generic JSON Schema objects with optional `$ref`.
|
|
183
|
+
*
|
|
184
|
+
* @param input - The value to check.
|
|
185
|
+
* @returns True if the input is a generic JSON Schema object, false otherwise.
|
|
186
|
+
*/
|
|
187
|
+
function isJsonSchemaAny(input) {
|
|
188
|
+
if (!isSetObject$1(input)) return false;
|
|
189
|
+
const schema = input;
|
|
190
|
+
return hasValidJsonSchemaKeywords(schema) && isOptionalString(schema.$ref) && (!schema.type || Array.isArray(schema.type) && schema.type.every((t) => JSON_SCHEMA_PRIMITIVE_TYPES.includes(t)));
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Type guard for JSON Schema array types.
|
|
194
|
+
*
|
|
195
|
+
* @param input - The value to check.
|
|
196
|
+
* @returns True if the input is a JSON Schema array schema, false otherwise.
|
|
197
|
+
*/
|
|
198
|
+
function isJsonSchemaArray(input) {
|
|
199
|
+
if (!isSetObject$1(input)) return false;
|
|
200
|
+
const schema = input;
|
|
201
|
+
if (!hasValidJsonSchemaKeywords(schema) || schema.type !== "array") return false;
|
|
202
|
+
return isOptionalJsonSchemaArray(schema.prefixItems) && isOptionalJsonSchema(schema.items) && isOptionalJsonSchema(schema.contains) && isOptionalNumber(schema.minItems) && isOptionalNumber(schema.maxItems) && isOptionalBoolean(schema.uniqueItems) && isOptionalNumber(schema.minContains) && isOptionalNumber(schema.maxContains) && (schema.unevaluatedItems === void 0 || isBoolean(schema.unevaluatedItems) || isJsonSchema(schema.unevaluatedItems));
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* Type guard for bigint-backed integer schemas.
|
|
206
|
+
*
|
|
207
|
+
* @param input - The value to check.
|
|
208
|
+
* @returns True if the input is a bigint-backed integer schema, false otherwise.
|
|
209
|
+
*/
|
|
210
|
+
function isJsonSchemaBigint(input) {
|
|
211
|
+
if (!isSetObject$1(input)) return false;
|
|
212
|
+
const schema = input;
|
|
213
|
+
if (!hasValidJsonSchemaKeywords(schema) || schema.type !== "integer" || schema.format !== "int64") return false;
|
|
214
|
+
return isOptionalBigint(schema.minimum) && isOptionalBigint(schema.exclusiveMinimum) && isOptionalBigint(schema.maximum) && isOptionalBigint(schema.exclusiveMaximum) && isOptionalBigint(schema.multipleOf) && isOptionalBigint(schema.default) && (schema.enum === void 0 || isArrayOf(schema.enum, isSetBigint));
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* Type guard for boolean schemas.
|
|
218
|
+
*
|
|
219
|
+
* @param input - The value to check.
|
|
220
|
+
* @returns True if the input is a boolean schema, false otherwise.
|
|
221
|
+
*/
|
|
222
|
+
function isJsonSchemaBoolean(input) {
|
|
223
|
+
if (!isSetObject$1(input)) return false;
|
|
224
|
+
const schema = input;
|
|
225
|
+
return hasValidJsonSchemaKeywords(schema) && schema.type === "boolean" && isOptionalBoolean(schema.default);
|
|
226
|
+
}
|
|
227
|
+
/**
|
|
228
|
+
* Type guard for date/time schemas.
|
|
229
|
+
*
|
|
230
|
+
* @param input - The value to check.
|
|
231
|
+
* @returns True if the input is a date or time schema, false otherwise.
|
|
232
|
+
*/
|
|
233
|
+
function isJsonSchemaDate(input) {
|
|
234
|
+
if (!isSetObject$1(input)) return false;
|
|
235
|
+
const schema = input;
|
|
236
|
+
if (!hasValidJsonSchemaKeywords(schema)) return false;
|
|
237
|
+
if (schema.anyOf !== void 0) return isArrayOf(schema.anyOf, isJsonSchemaDate);
|
|
238
|
+
if (schema.type !== "integer" && schema.type !== "string") return false;
|
|
239
|
+
if (!isSetString$1(schema.format) || !DATE_FORMAT_SET.has(schema.format)) return false;
|
|
240
|
+
return isOptionalNumber(schema.minimum) && isOptionalNumber(schema.maximum) && (schema.default === void 0 || isSetString$1(schema.default) || isSetNumber(schema.default));
|
|
241
|
+
}
|
|
242
|
+
/**
|
|
243
|
+
* Type guard for enum-constrained schemas.
|
|
244
|
+
*
|
|
245
|
+
* @param input - The value to check.
|
|
246
|
+
* @returns True if the input is an enum-constrained schema, false otherwise.
|
|
247
|
+
*/
|
|
248
|
+
function isJsonSchemaEnum(input) {
|
|
249
|
+
if (!isSetObject$1(input)) return false;
|
|
250
|
+
const schema = input;
|
|
251
|
+
if (!hasValidJsonSchemaKeywords(schema) || !isSetString$1(schema.type) || !JSON_SCHEMA_PRIMITIVE_TYPE_SET.has(schema.type) || !Array.isArray(schema.enum)) return false;
|
|
252
|
+
const typeName = schema.type;
|
|
253
|
+
const enumValues = schema.enum;
|
|
254
|
+
const defaultValue = schema.default;
|
|
255
|
+
if (typeName === "string") return enumValues.every((value) => isSetString$1(value)) && (defaultValue === void 0 || isSetString$1(defaultValue));
|
|
256
|
+
if (typeName === "number" || typeName === "integer") return enumValues.every((value) => isSetNumber(value)) && (defaultValue === void 0 || isSetNumber(defaultValue));
|
|
257
|
+
if (typeName === "boolean") return enumValues.every((value) => isBoolean(value)) && (defaultValue === void 0 || isBoolean(defaultValue));
|
|
258
|
+
return typeName === "null" && enumValues.every((value) => value === null) && (defaultValue === void 0 || defaultValue === null);
|
|
259
|
+
}
|
|
260
|
+
/**
|
|
261
|
+
* Type guard for `allOf` composition schemas.
|
|
262
|
+
*
|
|
263
|
+
* @param input - The value to check.
|
|
264
|
+
* @returns True if the input is an `allOf` schema, false otherwise.
|
|
265
|
+
*/
|
|
266
|
+
function isJsonSchemaAllOf(input) {
|
|
267
|
+
if (!isSetObject$1(input)) return false;
|
|
268
|
+
const schema = input;
|
|
269
|
+
if (!hasValidJsonSchemaKeywords(schema) || !isArrayOf(schema.allOf, isJsonSchema)) return false;
|
|
270
|
+
return schema.unevaluatedProperties === void 0 || isBoolean(schema.unevaluatedProperties) || isJsonSchema(schema.unevaluatedProperties);
|
|
271
|
+
}
|
|
272
|
+
/**
|
|
273
|
+
* Type guard for literal-value schemas.
|
|
274
|
+
*
|
|
275
|
+
* @param input - The value to check.
|
|
276
|
+
* @returns True if the input is a literal-value schema, false otherwise.
|
|
277
|
+
*/
|
|
278
|
+
function isJsonSchemaLiteral(input) {
|
|
279
|
+
if (!isSetObject$1(input)) return false;
|
|
280
|
+
const schema = input;
|
|
281
|
+
if (!hasValidJsonSchemaKeywords(schema) || !("const" in schema)) return false;
|
|
282
|
+
return schema.type === void 0 || isSetString$1(schema.type) && isJsonSchemaType(schema.type) || isOptionalJsonSchemaTypeArray(schema.type);
|
|
283
|
+
}
|
|
284
|
+
/**
|
|
285
|
+
* Type guard for map tuple-array schemas.
|
|
286
|
+
*
|
|
287
|
+
* @param input - The value to check.
|
|
288
|
+
* @returns True if the input is a map tuple-array schema, false otherwise.
|
|
289
|
+
*/
|
|
290
|
+
function isJsonSchemaMap(input) {
|
|
291
|
+
if (!isSetObject$1(input)) return false;
|
|
292
|
+
const schema = input;
|
|
293
|
+
if (!hasValidJsonSchemaKeywords(schema) || schema.type !== "array" || schema.maxItems !== 125 || !isSetObject$1(schema.items)) return false;
|
|
294
|
+
const items = schema.items;
|
|
295
|
+
return items.type === "array" && isTupleOfTwo(items.prefixItems, isJsonSchema, isJsonSchema) && (items.items === void 0 || items.items === false) && items.minItems === 2 && items.maxItems === 2;
|
|
296
|
+
}
|
|
297
|
+
/**
|
|
298
|
+
* Type guard for native enum schemas.
|
|
299
|
+
*
|
|
300
|
+
* @param input - The value to check.
|
|
301
|
+
* @returns True if the input is a native enum schema, false otherwise.
|
|
302
|
+
*/
|
|
303
|
+
function isJsonSchemaNativeEnum(input) {
|
|
304
|
+
if (!isSetObject$1(input)) return false;
|
|
305
|
+
const schema = input;
|
|
306
|
+
const isValidType = schema.type === "string" || schema.type === "number" || Array.isArray(schema.type) && schema.type.length === 2 && schema.type[0] === "string" && schema.type[1] === "number";
|
|
307
|
+
return hasValidJsonSchemaKeywords(schema) && isValidType && Array.isArray(schema.enum) && schema.enum.every((value) => isSetString$1(value) || isSetNumber(value));
|
|
308
|
+
}
|
|
309
|
+
/**
|
|
310
|
+
* Type guard for impossible/never schemas.
|
|
311
|
+
*
|
|
312
|
+
* @param input - The value to check.
|
|
313
|
+
* @returns True if the input is a never schema, false otherwise.
|
|
314
|
+
*/
|
|
315
|
+
function isJsonSchemaNever(input) {
|
|
316
|
+
if (!isSetObject$1(input)) return false;
|
|
317
|
+
const schema = input;
|
|
318
|
+
return hasValidJsonSchemaKeywords(schema) && isJsonSchemaAny(schema.not);
|
|
319
|
+
}
|
|
320
|
+
/**
|
|
321
|
+
* Type guard for `null` schemas.
|
|
322
|
+
*
|
|
323
|
+
* @param input - The value to check.
|
|
324
|
+
* @returns True if the input is a null schema, false otherwise.
|
|
325
|
+
*/
|
|
326
|
+
function isJsonSchemaNull(input) {
|
|
327
|
+
if (!isSetObject$1(input)) return false;
|
|
328
|
+
const schema = input;
|
|
329
|
+
return hasValidJsonSchemaKeywords(schema) && schema.type === "null" && (schema.const === void 0 || schema.const === null) && (schema.enum === void 0 || Array.isArray(schema.enum) && schema.enum.every((v) => v === null)) && (schema.default === void 0 || schema.default === null);
|
|
330
|
+
}
|
|
331
|
+
/**
|
|
332
|
+
* Type guard for nullable schema unions.
|
|
333
|
+
*
|
|
334
|
+
* @param input - The value to check.
|
|
335
|
+
* @returns True if the input is a nullable schema union, false otherwise.
|
|
336
|
+
*/
|
|
337
|
+
function isJsonSchemaNullable(input) {
|
|
338
|
+
if (!isSetObject$1(input)) return false;
|
|
339
|
+
const schema = input;
|
|
340
|
+
if (!hasValidJsonSchemaKeywords(schema)) return false;
|
|
341
|
+
const anyOfBranch = schema.anyOf !== void 0 && Array.isArray(schema.anyOf) && schema.anyOf.length === 2 && isJsonSchema(schema.anyOf[0]) && isJsonSchemaNull(schema.anyOf[1]);
|
|
342
|
+
const typeBranch = Array.isArray(schema.type) && schema.type.length === 2 && (schema.type[0] === "null" && isSetString$1(schema.type[1]) && schema.type[1] !== "null" && JSON_SCHEMA_TYPE_SET.has(schema.type[1]) || schema.type[1] === "null" && isSetString$1(schema.type[0]) && schema.type[0] !== "null" && JSON_SCHEMA_TYPE_SET.has(schema.type[0]));
|
|
343
|
+
return anyOfBranch || typeBranch;
|
|
344
|
+
}
|
|
345
|
+
/**
|
|
346
|
+
* Type guard for numeric schemas.
|
|
347
|
+
*
|
|
348
|
+
* @param input - The value to check.
|
|
349
|
+
* @returns True if the input is a numeric schema, false otherwise.
|
|
350
|
+
*/
|
|
351
|
+
function isJsonSchemaNumber(input) {
|
|
352
|
+
if (!isSetObject$1(input)) return false;
|
|
353
|
+
const schema = input;
|
|
354
|
+
if (!hasValidJsonSchemaKeywords(schema) || schema.type !== "number" && schema.type !== "integer") return false;
|
|
355
|
+
return isOptionalString(schema.format) && isOptionalNumber(schema.minimum) && isOptionalNumber(schema.exclusiveMinimum) && isOptionalNumber(schema.maximum) && isOptionalNumber(schema.exclusiveMaximum) && isOptionalNumber(schema.multipleOf) && isOptionalNumber(schema.default) && (schema.enum === void 0 || isArrayOf(schema.enum, isSetNumber));
|
|
356
|
+
}
|
|
357
|
+
/**
|
|
358
|
+
* Type guard for integer schemas.
|
|
359
|
+
*
|
|
360
|
+
* @param input - The value to check.
|
|
361
|
+
* @returns True if the input is an integer schema, false otherwise.
|
|
362
|
+
*/
|
|
363
|
+
function isJsonSchemaInteger(input) {
|
|
364
|
+
return isJsonSchemaNumber(input) && input.type === "integer";
|
|
365
|
+
}
|
|
366
|
+
/**
|
|
367
|
+
* Type guard for decimal schemas.
|
|
368
|
+
*
|
|
369
|
+
* @param input - The value to check.
|
|
370
|
+
* @returns True if the input is a decimal schema, false otherwise.
|
|
371
|
+
*/
|
|
372
|
+
function isJsonSchemaDecimal(input) {
|
|
373
|
+
return isJsonSchemaNumber(input) && input.type === "number";
|
|
374
|
+
}
|
|
375
|
+
/**
|
|
376
|
+
* Type guard for object schemas.
|
|
377
|
+
*
|
|
378
|
+
* @param input - The value to check.
|
|
379
|
+
* @returns True if the input is an object schema, false otherwise.
|
|
380
|
+
*/
|
|
381
|
+
function isJsonSchemaObject(input) {
|
|
382
|
+
if (!isSetObject$1(input)) return false;
|
|
383
|
+
const schema = input;
|
|
384
|
+
return hasValidJsonSchemaKeywords(schema) && schema.type === "object" && (schema.properties === void 0 || isRecordOfSchemaLike(schema.properties)) && (schema.patternProperties === void 0 || isRecordOfSchemaLike(schema.patternProperties)) && (schema.additionalProperties === void 0 || isBoolean(schema.additionalProperties) || isJsonSchema(schema.additionalProperties)) && (schema.required === void 0 || isStringArray(schema.required)) && (schema.unevaluatedProperties === void 0 || isBoolean(schema.unevaluatedProperties) || isJsonSchema(schema.unevaluatedProperties)) && (schema.dependencies === void 0 || isSetObject$1(schema.dependencies) && Object.values(schema.dependencies).every((item) => isStringArray(item) || isJsonSchema(item))) && (schema.dependentRequired === void 0 || isRecordOfStringArrays(schema.dependentRequired)) && (schema.dependentSchemas === void 0 || isRecordOfSchemaLike(schema.dependentSchemas)) && isOptionalNumber(schema.minProperties) && isOptionalNumber(schema.maxProperties) && (schema.primaryKey === void 0 || isStringArray(schema.primaryKey)) && isOptionalString(schema.databaseSchema);
|
|
385
|
+
}
|
|
386
|
+
/**
|
|
387
|
+
* Type guard for string schemas.
|
|
388
|
+
*
|
|
389
|
+
* @param input - The value to check.
|
|
390
|
+
* @returns True if the input is a string schema, false otherwise.
|
|
391
|
+
*/
|
|
392
|
+
function isJsonSchemaString(input) {
|
|
393
|
+
if (!isSetObject$1(input)) return false;
|
|
394
|
+
const schema = input;
|
|
395
|
+
return hasValidJsonSchemaKeywords(schema) && schema.type === "string" && isOptionalNumber(schema.minLength) && isOptionalNumber(schema.maxLength) && isOptionalString(schema.pattern) && isOptionalString(schema.format) && isOptionalString(schema.default) && (schema.enum === void 0 || isArrayOf(schema.enum, isSetString$1)) && isOptionalString(schema.contentMediaType) && isOptionalString(schema.contentEncoding) && isOptionalString(schema.contentSchema);
|
|
396
|
+
}
|
|
397
|
+
/**
|
|
398
|
+
* Type guard for set-like array schemas.
|
|
399
|
+
*
|
|
400
|
+
* @param input - The value to check.
|
|
401
|
+
* @returns True if the input is a set-like array schema, false otherwise.
|
|
402
|
+
*/
|
|
403
|
+
function isJsonSchemaSet(input) {
|
|
404
|
+
if (!isSetObject$1(input)) return false;
|
|
405
|
+
const schema = input;
|
|
406
|
+
return hasValidJsonSchemaKeywords(schema) && schema.type === "array" && schema.uniqueItems === true && isOptionalJsonSchemaArray(schema.prefixItems) && isOptionalJsonSchema(schema.items) && isOptionalJsonSchema(schema.contains) && isOptionalNumber(schema.minItems) && isOptionalNumber(schema.maxItems) && isOptionalNumber(schema.minContains) && isOptionalNumber(schema.maxContains) && (schema.unevaluatedItems === void 0 || isBoolean(schema.unevaluatedItems) || isJsonSchema(schema.unevaluatedItems));
|
|
407
|
+
}
|
|
408
|
+
/**
|
|
409
|
+
* Type guard for record schemas.
|
|
410
|
+
*
|
|
411
|
+
* @param input - The value to check.
|
|
412
|
+
* @returns True if the input is a record schema, false otherwise.
|
|
413
|
+
*/
|
|
414
|
+
function isJsonSchemaRecord(input) {
|
|
415
|
+
if (!isSetObject$1(input)) return false;
|
|
416
|
+
const schema = input;
|
|
417
|
+
return hasValidJsonSchemaKeywords(schema) && schema.type === "object" && (schema.patternProperties === void 0 || isRecordOfSchemaLike(schema.patternProperties)) && (schema.additionalProperties === void 0 || isBoolean(schema.additionalProperties) || isJsonSchema(schema.additionalProperties)) && isOptionalJsonSchema(schema.propertyNames);
|
|
418
|
+
}
|
|
419
|
+
/**
|
|
420
|
+
* Type guard for tuple schemas.
|
|
421
|
+
*
|
|
422
|
+
* @param input - The value to check.
|
|
423
|
+
* @returns True if the input is a tuple schema, false otherwise.
|
|
424
|
+
*/
|
|
425
|
+
function isJsonSchemaTuple(input) {
|
|
426
|
+
if (!isSetObject$1(input)) return false;
|
|
427
|
+
const schema = input;
|
|
428
|
+
return hasValidJsonSchemaKeywords(schema) && schema.type === "array" && isArrayOf(schema.prefixItems, isJsonSchema) && isOptionalNumber(schema.minItems) && isOptionalNumber(schema.maxItems) && isOptionalJsonSchema(schema.items) && isOptionalJsonSchema(schema.contains) && isOptionalBoolean(schema.uniqueItems) && isOptionalNumber(schema.minContains) && isOptionalNumber(schema.maxContains) && (schema.unevaluatedItems === void 0 || isBoolean(schema.unevaluatedItems) || isJsonSchema(schema.unevaluatedItems));
|
|
429
|
+
}
|
|
430
|
+
/**
|
|
431
|
+
* Type guard for undefined-representing schemas.
|
|
432
|
+
*
|
|
433
|
+
* @param input - The value to check.
|
|
434
|
+
* @returns True if the input is an undefined-representing schema, false otherwise.
|
|
435
|
+
*/
|
|
436
|
+
function isJsonSchemaUndefined(input) {
|
|
437
|
+
if (!isSetObject$1(input)) return false;
|
|
438
|
+
const schema = input;
|
|
439
|
+
return hasValidJsonSchemaKeywords(schema) && isJsonSchemaAny(schema.not) && (schema.default === void 0 || schema.default === void 0);
|
|
440
|
+
}
|
|
441
|
+
/**
|
|
442
|
+
* Type guard for primitive-union schema variants.
|
|
443
|
+
*
|
|
444
|
+
* @param input - The value to check.
|
|
445
|
+
* @returns True if the input is a primitive-union schema variant, false otherwise.
|
|
446
|
+
*/
|
|
447
|
+
function isJsonSchemaPrimitiveUnion(input) {
|
|
448
|
+
if (!isSetObject$1(input)) return false;
|
|
449
|
+
const schema = input;
|
|
450
|
+
if (!hasValidJsonSchemaKeywords(schema)) return false;
|
|
451
|
+
if (isSetString$1(schema.type) && JSON_SCHEMA_PRIMITIVE_TYPE_SET.has(schema.type)) {
|
|
452
|
+
if (schema.enum === void 0) return true;
|
|
453
|
+
if (!Array.isArray(schema.enum)) return false;
|
|
454
|
+
if (schema.type === "string") return schema.enum.every((value) => isSetString$1(value)) && (schema.default === void 0 || isSetString$1(schema.default));
|
|
455
|
+
if (schema.type === "number") return schema.enum.every((value) => isSetNumber(value)) && (schema.default === void 0 || isSetNumber(schema.default));
|
|
456
|
+
if (schema.type === "boolean") return schema.enum.every((value) => isBoolean(value)) && (schema.default === void 0 || isBoolean(schema.default));
|
|
457
|
+
if (schema.type === "integer") {
|
|
458
|
+
if (schema.format !== "int64") return false;
|
|
459
|
+
return schema.enum.every((value) => isSetBigint(value)) && (schema.default === void 0 || isSetBigint(schema.default));
|
|
460
|
+
}
|
|
461
|
+
return schema.type === "null" && schema.enum.every((value) => value === null) && (schema.default === void 0 || schema.default === null);
|
|
462
|
+
}
|
|
463
|
+
return isOptionalPrimitiveTypeArray(schema.type) && schema.type !== void 0;
|
|
464
|
+
}
|
|
465
|
+
/**
|
|
466
|
+
* Type guard for union schemas.
|
|
467
|
+
*
|
|
468
|
+
* @param input - The value to check.
|
|
469
|
+
* @returns True if the input is a union schema, false otherwise.
|
|
470
|
+
*/
|
|
471
|
+
function isJsonSchemaUnion(input) {
|
|
472
|
+
if (!isSetObject$1(input)) return false;
|
|
473
|
+
const schema = input;
|
|
474
|
+
return hasValidJsonSchemaKeywords(schema) && (isJsonSchemaPrimitiveUnion(schema) || isJsonSchemaAnyOf(schema));
|
|
475
|
+
}
|
|
476
|
+
/**
|
|
477
|
+
* Type guard for permissive unknown schemas.
|
|
478
|
+
*
|
|
479
|
+
* @param input - The value to check.
|
|
480
|
+
* @returns True if the input is a permissive unknown schema, false otherwise.
|
|
481
|
+
*/
|
|
482
|
+
function isJsonSchemaUnknown(input) {
|
|
483
|
+
return isJsonSchemaAny(input);
|
|
484
|
+
}
|
|
485
|
+
/**
|
|
486
|
+
* Type guard for `anyOf` composition schemas.
|
|
487
|
+
*
|
|
488
|
+
* @param input - The value to check.
|
|
489
|
+
* @returns True if the input is an `anyOf` schema, false otherwise.
|
|
490
|
+
*/
|
|
491
|
+
function isJsonSchemaAnyOf(input) {
|
|
492
|
+
if (!isSetObject$1(input)) return false;
|
|
493
|
+
const schema = input;
|
|
494
|
+
return hasValidJsonSchemaKeywords(schema) && isArrayOf(schema.anyOf, isJsonSchema);
|
|
495
|
+
}
|
|
496
|
+
/**
|
|
497
|
+
* Type guard for reference schemas.
|
|
498
|
+
*
|
|
499
|
+
* @param input - The value to check.
|
|
500
|
+
* @returns True if the input is a reference schema, false otherwise.
|
|
501
|
+
*/
|
|
502
|
+
function isJsonSchemaRef(input) {
|
|
503
|
+
if (!isSetObject$1(input)) return false;
|
|
504
|
+
const schema = input;
|
|
505
|
+
return hasValidJsonSchemaKeywords(schema) && isSetString$1(schema.$ref);
|
|
506
|
+
}
|
|
507
|
+
/**
|
|
508
|
+
* Type guard for Standard Schema V1 objects.
|
|
509
|
+
*
|
|
510
|
+
* @param input - The value to check.
|
|
511
|
+
* @returns True if the input is a Standard Schema V1 object, false otherwise.
|
|
512
|
+
*/
|
|
513
|
+
function isStandardSchema(input) {
|
|
514
|
+
if (!isSetObject$1(input)) return false;
|
|
515
|
+
const schema = input;
|
|
516
|
+
if (!isSetObject$1(schema["~standard"])) return false;
|
|
517
|
+
const standard = schema["~standard"];
|
|
518
|
+
return standard.version === 1 && isFunction(standard.validate);
|
|
519
|
+
}
|
|
520
|
+
/**
|
|
521
|
+
* Type guard for Valibot BaseSchema objects.
|
|
522
|
+
*
|
|
523
|
+
* @param input - The value to check.
|
|
524
|
+
* @returns True if the input is a Valibot BaseSchema, false otherwise.
|
|
525
|
+
*/
|
|
526
|
+
function isValibotSchema(input) {
|
|
527
|
+
if (!isSetObject$1(input) || !isStandardSchema(input)) return false;
|
|
528
|
+
const schema = input;
|
|
529
|
+
return schema.kind === "schema" && isSetString$1(schema.type) && isBoolean(schema.async) && isFunction(schema.reference) && isSetString$1(schema.expects) && isFunction(schema["~run"]);
|
|
530
|
+
}
|
|
531
|
+
/**
|
|
532
|
+
* Type guard for JSON Schema types.
|
|
533
|
+
*
|
|
534
|
+
* @remarks
|
|
535
|
+
* This function checks if the input is a JSON Schema type, which is defined as having a `type` property or a `$ref` property. This is used to determine if a given input conforms to the structure of a JSON Schema definition that includes type information.
|
|
536
|
+
*
|
|
537
|
+
* @param input - The value to check.
|
|
538
|
+
* @returns True if the input is a JSON Schema type, false otherwise.
|
|
539
|
+
*/
|
|
540
|
+
function isJsonSchema(input) {
|
|
541
|
+
return isJsonSchemaString(input) || isJsonSchemaInteger(input) || isJsonSchemaDecimal(input) || isJsonSchemaBigint(input) || isJsonSchemaBoolean(input) || isJsonSchemaDate(input) || isJsonSchemaEnum(input) || isJsonSchemaLiteral(input) || isJsonSchemaNativeEnum(input) || isJsonSchemaNull(input) || isJsonSchemaArray(input) || isJsonSchemaObject(input) || isJsonSchemaRecord(input) || isJsonSchemaTuple(input) || isJsonSchemaUnion(input) || isJsonSchemaUndefined(input) || isJsonSchemaRef(input) || isJsonSchemaNever(input) || isJsonSchemaMap(input) || isJsonSchemaAny(input) || isJsonSchemaNullable(input) || isJsonSchemaAllOf(input) || isJsonSchemaUnknown(input) || isJsonSchemaSet(input);
|
|
542
|
+
}
|
|
543
|
+
/**
|
|
544
|
+
* Type guard for JSON Schemas that only accept `null`.
|
|
545
|
+
*
|
|
546
|
+
* @remarks
|
|
547
|
+
* This function checks if the input is a JSON Schema that exclusively accepts the `null` type. It verifies that the input is a valid JSON Schema and that its `type` property is set to "null". This is useful for identifying schemas that are specifically designed to allow only `null` values.
|
|
548
|
+
*
|
|
549
|
+
* @param input - The value to check.
|
|
550
|
+
* @returns True if the input is a JSON Schema that only accepts `null`, false otherwise.
|
|
551
|
+
*/
|
|
552
|
+
function isNullOnlyJsonSchema(input) {
|
|
553
|
+
return isJsonSchemaNull(input);
|
|
554
|
+
}
|
|
555
|
+
/**
|
|
556
|
+
* Type guard for untyped schema objects.
|
|
557
|
+
*
|
|
558
|
+
* @remarks
|
|
559
|
+
* This function checks if the input is an untyped schema object, which is defined as having certain properties that are commonly found in untyped schema definitions. This includes properties such as `id`, `title`, `description`, `$schema`, `tsType`, `markdownType`, `type`, `required`, `tags`, `args`, `properties`, and `resolve`. The function verifies that these properties, if present, conform to the expected types (e.g., strings for certain properties, arrays for others). This type guard is used to determine if a given input can be treated as an untyped schema object within the context of the Powerlines schema system.
|
|
560
|
+
*
|
|
561
|
+
* @param input - The value to check.
|
|
562
|
+
* @returns True if the input is an untyped schema object, false otherwise.
|
|
563
|
+
*/
|
|
564
|
+
function isUntypedSchema(input) {
|
|
565
|
+
if (!isSetObject$1(input)) return false;
|
|
566
|
+
const schema = input;
|
|
567
|
+
if (!isUntypedTypeDescriptor(schema)) return false;
|
|
568
|
+
if ("id" in schema && !isSetString$1(schema.id)) return false;
|
|
569
|
+
if ("resolve" in schema && !isFunction(schema.resolve)) return false;
|
|
570
|
+
if ("properties" in schema && !isRecordOfUntypedSchema(schema.properties)) return false;
|
|
571
|
+
if ("required" in schema && !isStringArray(schema.required)) return false;
|
|
572
|
+
if ("title" in schema && !isSetString$1(schema.title)) return false;
|
|
573
|
+
if ("description" in schema && !isSetString$1(schema.description)) return false;
|
|
574
|
+
if ("$schema" in schema && !isSetString$1(schema.$schema)) return false;
|
|
575
|
+
if ("tags" in schema && !isStringArray(schema.tags)) return false;
|
|
576
|
+
if ("args" in schema && (!Array.isArray(schema.args) || !schema.args.every((item) => isUntypedFunctionArg(item)))) return false;
|
|
577
|
+
if ("returns" in schema && !isUntypedTypeDescriptor(schema.returns)) return false;
|
|
578
|
+
return true;
|
|
579
|
+
}
|
|
580
|
+
/**
|
|
581
|
+
* Strict type guard for untyped schema objects.
|
|
582
|
+
*
|
|
583
|
+
* @remarks
|
|
584
|
+
* This guard narrows values to the Untyped schema model while explicitly
|
|
585
|
+
* rejecting values that are also valid JSON Schema instances.
|
|
586
|
+
*
|
|
587
|
+
* @param input - The value to check.
|
|
588
|
+
* @returns True if the input is an untyped schema and not a JSON Schema.
|
|
589
|
+
*/
|
|
590
|
+
function isUntypedSchemaStrict(input) {
|
|
591
|
+
return isUntypedSchema(input) && !isJsonSchema(input);
|
|
592
|
+
}
|
|
593
|
+
/**
|
|
594
|
+
* Type guard for untyped input objects.
|
|
595
|
+
*
|
|
596
|
+
* @remarks
|
|
597
|
+
* This function checks if the input is an untyped input object, which is defined as having certain properties that are commonly found in untyped input definitions. This includes properties such as `$schema` and `$resolve`. The function verifies that these properties, if present, conform to the expected types (e.g., objects for `$schema`, functions for `$resolve`). This type guard is used to determine if a given input can be treated as an untyped input object within the context of the Powerlines schema system.
|
|
598
|
+
*
|
|
599
|
+
* @param input - The value to check.
|
|
600
|
+
* @returns True if the input is an untyped input object, false otherwise.
|
|
601
|
+
*/
|
|
602
|
+
function isUntypedConfig(input) {
|
|
603
|
+
if (!isSetObject$1(input)) return false;
|
|
604
|
+
const inputObject = input;
|
|
605
|
+
if ("$schema" in inputObject && !isUntypedSchema(inputObject.$schema)) return false;
|
|
606
|
+
if ("$resolve" in inputObject && !isFunction(inputObject.$resolve)) return false;
|
|
607
|
+
return true;
|
|
608
|
+
}
|
|
609
|
+
/**
|
|
610
|
+
* Strict type guard for untyped input objects.
|
|
611
|
+
*
|
|
612
|
+
* @remarks
|
|
613
|
+
* This guard narrows values to the Untyped input-object model while
|
|
614
|
+
* explicitly rejecting values that are valid JSON Schema objects.
|
|
615
|
+
*
|
|
616
|
+
* @param input - The value to check.
|
|
617
|
+
* @returns True if the input is an untyped input object and not JSON Schema.
|
|
618
|
+
*/
|
|
619
|
+
function isUntypedConfigStrict(input) {
|
|
620
|
+
if (!isUntypedConfig(input) || isJsonSchema(input)) return false;
|
|
621
|
+
const inputObject = input;
|
|
622
|
+
if ("$schema" in inputObject && inputObject.$schema !== void 0 && !isUntypedSchemaStrict(inputObject.$schema)) return false;
|
|
623
|
+
return true;
|
|
624
|
+
}
|
|
625
|
+
/**
|
|
626
|
+
* Type guard for Powerlines Schema objects.
|
|
627
|
+
*
|
|
628
|
+
* @param input - The value to check.
|
|
629
|
+
* @returns True if the input is a Powerlines Schema object, false otherwise.
|
|
630
|
+
*/
|
|
631
|
+
function isSchema(input) {
|
|
632
|
+
return isSetObject$1(input) && "schema" in input && isJsonSchema(input.schema) && "variant" in input && isSetString$1(input.variant) && "hash" in input && isSetString$1(input.hash);
|
|
633
|
+
}
|
|
634
|
+
/**
|
|
635
|
+
* Type guard for Powerlines Schema objects with a specific type specification.
|
|
636
|
+
*
|
|
637
|
+
* @param input - The value to check.
|
|
638
|
+
* @returns True if the input is a Powerlines Schema object with the specified type specification, false otherwise.
|
|
639
|
+
*/
|
|
640
|
+
function isSchemaOf(input) {
|
|
641
|
+
return isSchema(input);
|
|
642
|
+
}
|
|
643
|
+
/**
|
|
644
|
+
* Type guard for extracted schema objects that include source information.
|
|
645
|
+
*
|
|
646
|
+
* @param input - The value to check.
|
|
647
|
+
* @returns True if the input is a SchemaWithSource object, false otherwise.
|
|
648
|
+
*/
|
|
649
|
+
function isSchemaWithSource(input) {
|
|
650
|
+
return isSchema(input) && "source" in input && isSetObject$1(input.source) && "schema" in input.source && "variant" in input.source && isSetString$1(input.source.variant);
|
|
651
|
+
}
|
|
652
|
+
/**
|
|
653
|
+
* Type guard for Powerlines Schemas that are in Object form.
|
|
654
|
+
*
|
|
655
|
+
* @param input - The value to check.
|
|
656
|
+
* @returns True if the input is a Powerlines Schema object in Object form, false otherwise.
|
|
657
|
+
*/
|
|
658
|
+
function isSchemaObject(input) {
|
|
659
|
+
return isSchema(input) && isJsonSchemaObject(input.schema);
|
|
660
|
+
}
|
|
661
|
+
|
|
662
|
+
//#endregion
|
|
663
|
+
//#region src/helpers.ts
|
|
664
|
+
/**
|
|
665
|
+
* Retrieves the JSON Schema from a Schema wrapper or returns the input if it's already a JSON Schema.
|
|
666
|
+
*
|
|
667
|
+
* @remarks
|
|
668
|
+
* This function checks if the input is a Schema wrapper (an object with a `schema` property) and returns the `schema` if it is. If the input is already a JSON Schema, it returns it directly. This allows for flexibility in handling both raw JSON Schema objects and wrapped schemas without needing to check the type at every usage point.
|
|
669
|
+
*
|
|
670
|
+
* @param input - The input which can be either a Schema wrapper or a JSON Schema object.
|
|
671
|
+
* @returns The JSON Schema object.
|
|
672
|
+
* @throws Will throw a TypeError if the input is neither a valid Schema wrapper nor a valid JSON Schema object.
|
|
673
|
+
*/
|
|
674
|
+
function getJsonSchema(input) {
|
|
675
|
+
const schema = isSchema(input) ? input.schema : input;
|
|
676
|
+
if (!isJsonSchema(schema)) throw new TypeError(`The provided input is not a valid JSON Schema`);
|
|
677
|
+
return schema;
|
|
678
|
+
}
|
|
679
|
+
/**
|
|
680
|
+
* Retrieves the JSON Schema in Object form from a Schema wrapper or returns the input if it's already a JSON Schema.
|
|
681
|
+
*
|
|
682
|
+
* @remarks
|
|
683
|
+
* This function checks if the input is a Schema wrapper (an object with a `schema` property) and returns the `schema` if it is. If the input is already a JSON Schema object, it returns it directly. This allows for flexibility in handling both raw JSON Schema objects and wrapped schemas without needing to check the type at every usage point.
|
|
684
|
+
*
|
|
685
|
+
* @param input - The input which can be either a Schema wrapper or a JSON Schema object.
|
|
686
|
+
* @returns The JSON Schema object.
|
|
687
|
+
* @throws Will throw a TypeError if the input is neither a valid Schema wrapper nor a valid JSON Schema object.
|
|
688
|
+
*/
|
|
689
|
+
function getJsonSchemaObject(input) {
|
|
690
|
+
const schema = getJsonSchema(input);
|
|
691
|
+
if (!isJsonSchemaObject(schema)) throw new TypeError(`The provided input is not a valid JSON Schema object`);
|
|
692
|
+
return schema;
|
|
693
|
+
}
|
|
694
|
+
/**
|
|
695
|
+
* Extracts object properties from a JSON Schema object form.
|
|
696
|
+
*
|
|
697
|
+
* @remarks
|
|
698
|
+
* This function returns an empty object if the schema is not an object form or if it has no properties.
|
|
699
|
+
*
|
|
700
|
+
* @param obj - The JSON Schema object form or a Schema wrapper to extract properties from.
|
|
701
|
+
* @returns An object mapping property names to their corresponding JSON Schema fragments, including metadata.
|
|
702
|
+
*/
|
|
703
|
+
function getProperties(obj) {
|
|
704
|
+
const schema = getJsonSchemaObject(obj);
|
|
705
|
+
if (!isSetObject$1(schema.properties)) return {};
|
|
706
|
+
return Object.entries(schema.properties).reduce((ret, [key, value]) => {
|
|
707
|
+
value.name = key;
|
|
708
|
+
value.required = !isPropertyOptional(schema, key);
|
|
709
|
+
value.default = schema.default?.[key] ?? value.default;
|
|
710
|
+
ret[key] = value;
|
|
711
|
+
return ret;
|
|
712
|
+
}, {});
|
|
713
|
+
}
|
|
714
|
+
/**
|
|
715
|
+
* Returns object properties as an array.
|
|
716
|
+
*
|
|
717
|
+
* @remarks
|
|
718
|
+
* This is a convenience function that extracts properties using `getProperties` and returns them as an array.
|
|
719
|
+
*
|
|
720
|
+
* @param obj - The JSON Schema object form or a Schema wrapper to extract properties from.
|
|
721
|
+
* @returns An array of JSON Schema fragments representing the properties, each including metadata.
|
|
722
|
+
*/
|
|
723
|
+
function getPropertiesList(obj) {
|
|
724
|
+
return Object.values(getProperties(obj));
|
|
725
|
+
}
|
|
726
|
+
/**
|
|
727
|
+
* Extracts object properties from a JSON Schema object form.
|
|
728
|
+
*
|
|
729
|
+
* @remarks
|
|
730
|
+
* This function returns an empty object if the schema is not an object form or if it has no properties.
|
|
731
|
+
*
|
|
732
|
+
* @param obj - The JSON Schema object form or a Schema wrapper to extract properties from.
|
|
733
|
+
* @returns An object mapping property names to their corresponding JSON Schema fragments, including metadata.
|
|
734
|
+
*/
|
|
735
|
+
function getProperty(obj, name) {
|
|
736
|
+
const schema = getJsonSchemaObject(obj);
|
|
737
|
+
if (!isSetObject$1(schema.properties)) throw new TypeError(`The provided schema does not have any properties`);
|
|
738
|
+
const property = schema.properties[name];
|
|
739
|
+
if (!property) throw new TypeError(`The provided schema does not have a property named "${name}"`);
|
|
740
|
+
return {
|
|
741
|
+
...property,
|
|
742
|
+
name,
|
|
743
|
+
required: !isPropertyOptional(schema, name),
|
|
744
|
+
default: schema.default?.[name] ?? property?.default
|
|
745
|
+
};
|
|
746
|
+
}
|
|
747
|
+
/**
|
|
748
|
+
* Adds a property to a JSON Schema object form.
|
|
749
|
+
*
|
|
750
|
+
* @remarks
|
|
751
|
+
* This function modifies the provided schema in place by adding a new property with the specified name and schema. It also updates the `required` array based on the `optional` flag of the property. If the property is marked as optional, it will be removed from the `required` array; otherwise, it will be added to it.
|
|
752
|
+
*
|
|
753
|
+
* @param obj - The JSON Schema object form or a Schema wrapper to which the property should be added.
|
|
754
|
+
* @param name - The name of the property to add.
|
|
755
|
+
* @param property - The JSON Schema fragment representing the property's schema, including metadata.
|
|
756
|
+
* @throws Will throw an error if the provided schema is not an object form.
|
|
757
|
+
*/
|
|
758
|
+
function addProperty(obj, name, property) {
|
|
759
|
+
const schema = getJsonSchemaObject(obj);
|
|
760
|
+
schema.properties ??= {};
|
|
761
|
+
schema.required ??= [];
|
|
762
|
+
property.name = name;
|
|
763
|
+
schema.properties[name] = property;
|
|
764
|
+
if (!schema.required.includes(name)) schema.required.push(name);
|
|
765
|
+
if (schema.required.length === 0) delete schema.required;
|
|
766
|
+
}
|
|
767
|
+
/**
|
|
768
|
+
* Recursively merges two JSON Schema fragments. `override` wins for any scalar
|
|
769
|
+
* key that both schemas define, while structured keywords are handled
|
|
770
|
+
* specially:
|
|
771
|
+
*
|
|
772
|
+
* - `properties`, `patternProperties`, `$defs`, `definitions`,
|
|
773
|
+
* `dependentSchemas` — each matching child schema is merged recursively.
|
|
774
|
+
* - `allOf`, `anyOf`, `oneOf` — arrays are concatenated.
|
|
775
|
+
* - `if`, `then`, `else`, `not`, `contains`, `items`,
|
|
776
|
+
* `additionalProperties`, `unevaluatedProperties`, `propertyNames`,
|
|
777
|
+
* `unevaluatedItems` — merged recursively when both sides are schemas.
|
|
778
|
+
* - `required` — arrays are union'ed and deduplicated.
|
|
779
|
+
*/
|
|
780
|
+
function mergeTwo(base, override) {
|
|
781
|
+
const result = { ...base };
|
|
782
|
+
for (const [key, overrideValue] of Object.entries(override)) {
|
|
783
|
+
const baseValue = result[key];
|
|
784
|
+
if (key === "required") result[key] = getUnique([...Array.isArray(baseValue) ? baseValue : [], ...Array.isArray(overrideValue) ? overrideValue : []]);
|
|
785
|
+
else if (SCHEMA_RECORD_KEYWORDS.has(key) && isSetObject$1(baseValue) && isSetObject$1(overrideValue)) {
|
|
786
|
+
const merged = { ...baseValue };
|
|
787
|
+
for (const [childKey, childOverride] of Object.entries(overrideValue)) {
|
|
788
|
+
const childBase = merged[childKey];
|
|
789
|
+
merged[childKey] = isJsonSchema(childBase) && isJsonSchema(childOverride) ? mergeTwo(childBase, childOverride) : childOverride;
|
|
790
|
+
}
|
|
791
|
+
result[key] = merged;
|
|
792
|
+
} else if (SCHEMA_ARRAY_CONCAT_KEYWORDS.has(key) && Array.isArray(baseValue) && Array.isArray(overrideValue)) result[key] = [...baseValue, ...overrideValue];
|
|
793
|
+
else if (SCHEMA_SINGLE_KEYWORDS.has(key) && isJsonSchema(baseValue) && isJsonSchema(overrideValue)) result[key] = mergeTwo(baseValue, overrideValue);
|
|
794
|
+
else result[key] = overrideValue;
|
|
795
|
+
}
|
|
796
|
+
return result;
|
|
797
|
+
}
|
|
798
|
+
/**
|
|
799
|
+
* Merges multiple JSON Schemas into one.
|
|
800
|
+
*
|
|
801
|
+
* @remarks
|
|
802
|
+
* This function takes multiple JSON Schemas or Schema wrappers and merges them
|
|
803
|
+
* into a single JSON Schema object. Later schemas in the argument list take
|
|
804
|
+
* precedence over earlier ones for scalar conflicts.
|
|
805
|
+
*
|
|
806
|
+
* Structured keywords are merged recursively:
|
|
807
|
+
* - Named child schemas (`properties`, `$defs`, etc.) are merged
|
|
808
|
+
* per-property via recursive calls to `merge`.
|
|
809
|
+
* - Composition arrays (`allOf`, `anyOf`, `oneOf`) are concatenated.
|
|
810
|
+
* - Single-schema keywords (`if`, `then`, `else`, `not`, `items`, etc.)
|
|
811
|
+
* are merged recursively when both sides define them.
|
|
812
|
+
* - `required` arrays are union'ed and deduplicated.
|
|
813
|
+
*
|
|
814
|
+
* @param schemas - An array of JSON Schemas or Schema wrappers to merge.
|
|
815
|
+
* @returns A new JSON Schema that is the result of merging all input schemas.
|
|
816
|
+
*/
|
|
817
|
+
function merge(...schemas) {
|
|
818
|
+
const jsonSchemas = schemas.map((s) => getJsonSchema(s));
|
|
819
|
+
if (jsonSchemas.length === 0) return {};
|
|
820
|
+
return jsonSchemas.reduce((acc, schema) => {
|
|
821
|
+
const accType = acc.type;
|
|
822
|
+
const schemaType = schema.type;
|
|
823
|
+
if (accType && schemaType && accType !== schemaType) return schema;
|
|
824
|
+
return mergeTwo(acc, schema);
|
|
825
|
+
});
|
|
826
|
+
}
|
|
827
|
+
/**
|
|
828
|
+
* Merges metadata into a JSON Schema fragment.
|
|
829
|
+
*
|
|
830
|
+
* @remarks
|
|
831
|
+
* This function takes a JSON Schema fragment and a metadata object, and merges the metadata into the schema. Only the keys defined in {@link JSON_SCHEMA_METADATA_KEYS} are considered for merging. The resulting schema will include the original schema properties along with the provided metadata.
|
|
832
|
+
*
|
|
833
|
+
* @param schema - The JSON Schema fragment to merge metadata into.
|
|
834
|
+
* @param metadata - The metadata to merge into the schema.
|
|
835
|
+
* @returns A new JSON Schema fragment that includes the merged metadata.
|
|
836
|
+
*/
|
|
837
|
+
function mergeMetadata(schema, metadata) {
|
|
838
|
+
return defu(schema, Object.fromEntries(JSON_SCHEMA_METADATA_KEYS.filter((key) => metadata[key] !== void 0).map((key) => [key, metadata[key]])));
|
|
839
|
+
}
|
|
840
|
+
/**
|
|
841
|
+
* Returns whether a JSON Schema fragment accepts `null`.
|
|
842
|
+
*
|
|
843
|
+
* @remarks
|
|
844
|
+
* This is true if the schema has `nullable: true` or if its `type` includes `"null"`.
|
|
845
|
+
*
|
|
846
|
+
* @param schema - The JSON Schema fragment to check.
|
|
847
|
+
* @returns `true` if the schema accepts `null`, otherwise `false`.
|
|
848
|
+
*/
|
|
849
|
+
function isSchemaNullable(schema) {
|
|
850
|
+
if (!isSetObject$1(schema)) return false;
|
|
851
|
+
if (schema.nullable === true) return true;
|
|
852
|
+
return readSchemaTypes(schema).includes("null");
|
|
853
|
+
}
|
|
854
|
+
/**
|
|
855
|
+
* Returns whether an object property is optional (not listed in `required`).
|
|
856
|
+
*
|
|
857
|
+
* @remarks
|
|
858
|
+
* In JSON Schema, object properties are optional by default unless they are listed in the `required` array of the parent schema. This function checks whether a given property name is not included in the `required` array of its parent schema, indicating that it is optional.
|
|
859
|
+
*
|
|
860
|
+
* @param parent - The parent JSON Schema object containing the property.
|
|
861
|
+
* @param propertyName - The name of the property to check for optionality.
|
|
862
|
+
* @returns `true` if the property is optional, otherwise `false`.
|
|
863
|
+
*/
|
|
864
|
+
function isPropertyOptional(parent, propertyName) {
|
|
865
|
+
if (!parent.properties?.[propertyName]) throw new Error(`The property "${propertyName}" does not exist in the parent schema.`);
|
|
866
|
+
return !(parent.required ?? []).includes(propertyName);
|
|
867
|
+
}
|
|
868
|
+
/**
|
|
869
|
+
* Checks if a given file name has a valid schema input file extension.
|
|
870
|
+
*
|
|
871
|
+
* @param fileName - The file name to check for a valid schema input extension.
|
|
872
|
+
* @returns `true` if the file name has a valid schema input extension, otherwise `false`.
|
|
873
|
+
*/
|
|
874
|
+
function isValidSchemaConfigFile(fileName) {
|
|
875
|
+
return VALID_OBJECT_SOURCE_EXTENSIONS.includes(findFileExtensionSafe(fileName));
|
|
876
|
+
}
|
|
877
|
+
/**
|
|
878
|
+
* Traverses a JSON Schema and applies a callback function to each schema fragment.
|
|
879
|
+
*
|
|
880
|
+
* @remarks
|
|
881
|
+
* This function recursively traverses a JSON Schema, including nested schemas in properties, composition keywords (`allOf`, `anyOf`, `oneOf`), and other schema keywords. The provided callback function is invoked for each schema fragment encountered during the traversal. The results of the callback are collected and returned as an array.
|
|
882
|
+
*
|
|
883
|
+
* @param schema - The JSON Schema to traverse.
|
|
884
|
+
* @param callback - The callback function to apply to each schema fragment.
|
|
885
|
+
* @returns An array of results returned by the callback function.
|
|
886
|
+
*/
|
|
887
|
+
function traverseSchema(schema, callback) {
|
|
888
|
+
const results = [];
|
|
889
|
+
function traverse(schema) {
|
|
890
|
+
const result = callback(schema);
|
|
891
|
+
if (result !== void 0) results.push(result);
|
|
892
|
+
if (isJsonSchemaObject(schema)) for (const propSchema of Object.values(schema.properties ?? {})) traverse(propSchema);
|
|
893
|
+
for (const keyword of [
|
|
894
|
+
"allOf",
|
|
895
|
+
"anyOf",
|
|
896
|
+
"oneOf",
|
|
897
|
+
"not",
|
|
898
|
+
"if",
|
|
899
|
+
"then",
|
|
900
|
+
"else",
|
|
901
|
+
"$ref",
|
|
902
|
+
"items",
|
|
903
|
+
"additionalProperties",
|
|
904
|
+
"unevaluatedProperties",
|
|
905
|
+
"propertyNames",
|
|
906
|
+
"unevaluatedItems",
|
|
907
|
+
"contains"
|
|
908
|
+
]) {
|
|
909
|
+
const value = schema[keyword];
|
|
910
|
+
if (Array.isArray(value)) {
|
|
911
|
+
for (const subSchema of value) if (isJsonSchema(subSchema)) traverse(subSchema);
|
|
912
|
+
} else if (isJsonSchema(value)) traverse(value);
|
|
913
|
+
}
|
|
914
|
+
}
|
|
915
|
+
traverse(schema);
|
|
916
|
+
return results;
|
|
917
|
+
}
|
|
918
|
+
|
|
919
|
+
//#endregion
|
|
920
|
+
export { isUntypedSchema as $, isJsonSchemaNull as A, isJsonSchemaTuple as B, isJsonSchemaEnum as C, isJsonSchemaMap as D, isJsonSchemaLiteral as E, isJsonSchemaPrimitiveUnion as F, isNullOnlyJsonSchema as G, isJsonSchemaUndefined as H, isJsonSchemaRecord as I, isSchemaOf as J, isSchema as K, isJsonSchemaRef as L, isJsonSchemaNumber as M, isJsonSchemaObject as N, isJsonSchemaNativeEnum as O, isJsonSchemaPrimitiveType as P, isUntypedConfigStrict as Q, isJsonSchemaSet as R, isJsonSchemaDecimal as S, isJsonSchemaKeywords as T, isJsonSchemaUnion as U, isJsonSchemaType as V, isJsonSchemaUnknown as W, isStandardSchema as X, isSchemaWithSource as Y, isUntypedConfig as Z, isJsonSchemaAnyOf as _, getPropertiesList as a, isJsonSchemaBoolean as b, isSchemaNullable as c, mergeMetadata as d, isUntypedSchemaStrict as et, traverseSchema as f, isJsonSchemaAny as g, isJsonSchemaAllOf as h, getProperties as i, readSchemaTypes as it, isJsonSchemaNullable as j, isJsonSchemaNever as k, isValidSchemaConfigFile as l, isJsonSchema as m, getJsonSchema as n, applyJsonSchemaMetadata as nt, getProperty as o, isFileReference as p, isSchemaObject as q, getJsonSchemaObject as r, getPrimarySchemaType as rt, isPropertyOptional as s, addProperty as t, isValibotSchema as tt, merge as u, isJsonSchemaArray as v, isJsonSchemaInteger as w, isJsonSchemaDate as x, isJsonSchemaBigint as y, isJsonSchemaString as z };
|
|
921
|
+
//# sourceMappingURL=helpers-reqtcaa8.mjs.map
|