@stll/agent-input 0.2.2 → 0.4.0
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/country.d.ts +3 -2
- package/dist/index.d.ts +38 -3
- package/dist/index.js +434 -30
- package/dist/numeric-token.d.ts +2 -1
- package/package.json +3 -3
package/dist/country.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { t as Normalized } from "./normalized-B9RxhmIz.js";
|
|
2
1
|
import { CountryAlpha3Code, CountryCode } from "@stll/country-codes";
|
|
2
|
+
import { t as Normalized } from "./normalized-B9RxhmIz.js";
|
|
3
3
|
//#region src/country.d.ts
|
|
4
4
|
/**
|
|
5
5
|
* Jurisdictions that publish law without being ISO 3166-1 countries.
|
|
@@ -62,4 +62,5 @@ export declare const countryCodeIn: (value: CountryValue, spelling?: CountrySpel
|
|
|
62
62
|
* question about Czech law with Slovak decisions.
|
|
63
63
|
*/
|
|
64
64
|
export declare const normalizeCountry: (input: unknown, options?: CountryOptions) => Normalized<CountryValue>;
|
|
65
|
-
//#endregion
|
|
65
|
+
//#endregion
|
|
66
|
+
export {};
|
package/dist/index.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { a as askForFix, i as NormalizedOptional, n as NormalizedAbsent, o as askSentence, r as NormalizedAsk, s as readAsAbsent, t as Normalized } from "./normalized-B9RxhmIz.js";
|
|
2
1
|
import { COUNTRY_INPUT_MAX_CHARS, CountryAlpha2, CountryAlpha3, CountryOptions, CountrySpelling, CountryValue, countryCodeIn, normalizeCountry } from "./country.js";
|
|
3
2
|
import { DATE_VALUE_HINT, DateBound, DateBoundOptions, DateValueOptions, normalizeDateBound, normalizeDateValue } from "./date-value.js";
|
|
4
|
-
import { isPlausibleLocale, normalizeLocale } from "./locale.js";
|
|
5
|
-
import { StringListOptions, normalizeStringList } from "./string-list.js";
|
|
6
3
|
import { DateFormatStyle } from "@stll/template-conditions";
|
|
4
|
+
import { StringListOptions, normalizeStringList } from "./string-list.js";
|
|
5
|
+
import { a as askForFix, i as NormalizedOptional, n as NormalizedAbsent, o as askSentence, r as NormalizedAsk, s as readAsAbsent, t as Normalized } from "./normalized-B9RxhmIz.js";
|
|
6
|
+
import { isPlausibleLocale, normalizeLocale } from "./locale.js";
|
|
7
7
|
//#region src/absent.d.ts
|
|
8
8
|
/**
|
|
9
9
|
* Placeholders on optional filters.
|
|
@@ -139,6 +139,9 @@ export declare const AGENT_INPUT_NORMALIZATION_KEY = "x-stella-agent-input";
|
|
|
139
139
|
export declare const AGENT_INPUT_NORMALIZATION_KIND: {
|
|
140
140
|
readonly boolean: "boolean";
|
|
141
141
|
readonly country: "country";
|
|
142
|
+
readonly currency: "currency";
|
|
143
|
+
readonly dataBox: "data-box";
|
|
144
|
+
readonly email: "email";
|
|
142
145
|
readonly date: "date";
|
|
143
146
|
readonly dateFormat: "date-format";
|
|
144
147
|
readonly eli: "eli";
|
|
@@ -146,6 +149,7 @@ export declare const AGENT_INPUT_NORMALIZATION_KIND: {
|
|
|
146
149
|
readonly filter: "filter";
|
|
147
150
|
readonly locale: "locale";
|
|
148
151
|
readonly number: "number";
|
|
152
|
+
readonly slug: "slug";
|
|
149
153
|
readonly stringList: "string-list";
|
|
150
154
|
readonly uuid: "uuid";
|
|
151
155
|
};
|
|
@@ -243,4 +247,35 @@ export declare const normalizeAgentInput: ({ path, placeholders, readers, schema
|
|
|
243
247
|
value: unknown;
|
|
244
248
|
}) => AgentInputNormalizationResult;
|
|
245
249
|
//#endregion
|
|
250
|
+
//#region src/null-optionals.d.ts
|
|
251
|
+
/**
|
|
252
|
+
* Apply the common optional-null rule before any agent-value coercion. The
|
|
253
|
+
* decision comes from the same schema validation will use: nullable values keep
|
|
254
|
+
* null, required values still fail, and optional non-null values read null as
|
|
255
|
+
* omission at every declared object level.
|
|
256
|
+
*/
|
|
257
|
+
export declare const withNullOptionalsOmitted: (schema: unknown, value: unknown) => unknown;
|
|
258
|
+
/**
|
|
259
|
+
* The same rule for a model's tool input, which also fills an optional string
|
|
260
|
+
* it is not setting with "": that reads as omitted too, but only where the
|
|
261
|
+
* property's own schema refuses "". Where "" is a value the property
|
|
262
|
+
* accepts, it stays exactly as sent. This is the placeholder rule the MCP
|
|
263
|
+
* input schemas apply (`nullAsAbsent`), read off JSON Schema.
|
|
264
|
+
*/
|
|
265
|
+
export declare const withModelPlaceholdersOmitted: (schema: unknown, value: unknown) => unknown;
|
|
266
|
+
/**
|
|
267
|
+
* `schema` with every optional property also admitting `null`, at every level
|
|
268
|
+
* `withModelPlaceholdersOmitted` reads: the spelling a model can use for "not
|
|
269
|
+
* set" on a route that treats every declared field as one to fill. Read
|
|
270
|
+
* against the original schema, `withModelPlaceholdersOmitted` drops exactly
|
|
271
|
+
* those nulls again, so the input a tool receives keeps its declared shape.
|
|
272
|
+
*/
|
|
273
|
+
export declare const withOptionalsNullable: (schema: unknown) => unknown;
|
|
274
|
+
//#endregion
|
|
275
|
+
//#region src/email-address.d.ts
|
|
276
|
+
export declare const EMAIL_ADDRESS_PATTERN: RegExp;
|
|
277
|
+
//#endregion
|
|
278
|
+
//#region src/data-box-id.d.ts
|
|
279
|
+
export declare const DATA_BOX_ID_PATTERN: RegExp;
|
|
280
|
+
//#endregion
|
|
246
281
|
export { type AgentInputCountryAnnotation, type AgentInputNormalizationAnnotation, type AgentInputNormalizationIssue, type AgentInputNormalizationKind, type AgentInputNormalizationResult, type AgentInputPlaceholderPolicy, type AgentInputReaders, COUNTRY_INPUT_MAX_CHARS, type CountryAlpha2, type CountryAlpha3, type CountryOptions, type CountrySpelling, type CountryValue, DATE_VALUE_HINT, type DateBound, type DateBoundOptions, type DateValueOptions, type EliOptions, type Normalized, type NormalizedAbsent, type NormalizedAsk, type NormalizedOptional, type NumberInRangeOptions, type NumberOptions, type StringListOptions, type UuidOptions, type VocabularyEntry, type VocabularyOptions, askForFix, askSentence, countryCodeIn, isPlausibleLocale, normalizeCountry, normalizeDateBound, normalizeDateValue, normalizeLocale, normalizeStringList, readAsAbsent };
|
package/dist/index.js
CHANGED
|
@@ -123,7 +123,7 @@ const normalizeEnumValue = (input, allowed, options) => {
|
|
|
123
123
|
* is missing, except `iso`, whose output has none.
|
|
124
124
|
*/
|
|
125
125
|
/** Narrow `unknown` to a plain object (not null, not array). */
|
|
126
|
-
const isRecord$
|
|
126
|
+
const isRecord$2 = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
127
127
|
/** What a spec means when it names only a locale. */
|
|
128
128
|
const DEFAULT_DATE_FORMAT_STYLE = "long";
|
|
129
129
|
const quoted = (values) => values.map((value) => `"${value}"`).join(", ");
|
|
@@ -220,7 +220,7 @@ const readObjectSpec = (input) => {
|
|
|
220
220
|
* the wire object. */
|
|
221
221
|
const normalizeDateFormatSpec = (input) => {
|
|
222
222
|
if (typeof input === "string") return readStringSpec(input);
|
|
223
|
-
if (isRecord$
|
|
223
|
+
if (isRecord$2(input)) return readObjectSpec(input);
|
|
224
224
|
return specAsk(input);
|
|
225
225
|
};
|
|
226
226
|
//#endregion
|
|
@@ -663,11 +663,59 @@ const normalizeVocabularyValue = (input, entries, options = {}) => {
|
|
|
663
663
|
return otherContaining.length === 0 ? readValueAs(input, onlyContaining) : askAmong(containing);
|
|
664
664
|
};
|
|
665
665
|
//#endregion
|
|
666
|
+
//#region src/currency-code.ts
|
|
667
|
+
const normalizeCurrencyCode = (input) => {
|
|
668
|
+
const value = typeof input === "string" ? input.trim().toUpperCase() : void 0;
|
|
669
|
+
return value !== void 0 && /^[A-Z]{3}$/u.test(value) ? readValueAs(input, value) : askForFix({
|
|
670
|
+
input,
|
|
671
|
+
expected: "a three-letter currency code",
|
|
672
|
+
hint: "Pass a three-letter currency code, such as EUR or CZK."
|
|
673
|
+
});
|
|
674
|
+
};
|
|
675
|
+
//#endregion
|
|
676
|
+
//#region src/data-box-id.ts
|
|
677
|
+
const DATA_BOX_ID_PATTERN = /^[A-Za-z0-9]{7}$/u;
|
|
678
|
+
/** Data box identifiers are case-sensitive external keys, never record UUIDs. */
|
|
679
|
+
const normalizeDataBoxId = (input) => {
|
|
680
|
+
const value = typeof input === "string" ? input.trim() : void 0;
|
|
681
|
+
return value !== void 0 && DATA_BOX_ID_PATTERN.test(value) ? readValueAs(input, value) : askForFix({
|
|
682
|
+
input,
|
|
683
|
+
expected: "a seven-character data box ID",
|
|
684
|
+
hint: "Pass the seven letters and digits of the recipient's data box ID."
|
|
685
|
+
});
|
|
686
|
+
};
|
|
687
|
+
//#endregion
|
|
688
|
+
//#region src/email-address.ts
|
|
689
|
+
const EMAIL_ADDRESS_PATTERN = /^[\w+-]+(?:\.[\w+-]+)*@[\da-zA-Z]+(?:[.-][\da-zA-Z]+)*\.[a-zA-Z]{2,}$/u;
|
|
690
|
+
/** Preserve the local part's case: contact addresses are not account identity keys. */
|
|
691
|
+
const normalizeEmailAddress = (input) => {
|
|
692
|
+
const value = typeof input === "string" ? input.trim() : void 0;
|
|
693
|
+
return value !== void 0 && value.length <= 320 && EMAIL_ADDRESS_PATTERN.test(value) ? readValueAs(input, value) : askForFix({
|
|
694
|
+
input,
|
|
695
|
+
expected: "an email address",
|
|
696
|
+
hint: "Pass one email address, such as name@example.com, without a display name."
|
|
697
|
+
});
|
|
698
|
+
};
|
|
699
|
+
//#endregion
|
|
700
|
+
//#region src/slug.ts
|
|
701
|
+
/** Case and internal separators identify a different slug; only outer whitespace is noise. */
|
|
702
|
+
const normalizeSlug = (input) => {
|
|
703
|
+
if (typeof input === "string" && /^[a-z0-9-]+$/u.test(input.trim())) return readValueAs(input, input.trim());
|
|
704
|
+
return askForFix({
|
|
705
|
+
input,
|
|
706
|
+
expected: "a lowercase slug",
|
|
707
|
+
hint: "Use lowercase ASCII letters, digits or hyphens, without spaces."
|
|
708
|
+
});
|
|
709
|
+
};
|
|
710
|
+
//#endregion
|
|
666
711
|
//#region src/schema.ts
|
|
667
712
|
const AGENT_INPUT_NORMALIZATION_KEY = "x-stella-agent-input";
|
|
668
713
|
const AGENT_INPUT_NORMALIZATION_KIND = {
|
|
669
714
|
boolean: "boolean",
|
|
670
715
|
country: "country",
|
|
716
|
+
currency: "currency",
|
|
717
|
+
dataBox: "data-box",
|
|
718
|
+
email: "email",
|
|
671
719
|
date: "date",
|
|
672
720
|
dateFormat: "date-format",
|
|
673
721
|
eli: "eli",
|
|
@@ -675,6 +723,7 @@ const AGENT_INPUT_NORMALIZATION_KIND = {
|
|
|
675
723
|
filter: "filter",
|
|
676
724
|
locale: "locale",
|
|
677
725
|
number: "number",
|
|
726
|
+
slug: "slug",
|
|
678
727
|
stringList: "string-list",
|
|
679
728
|
uuid: "uuid"
|
|
680
729
|
};
|
|
@@ -685,6 +734,9 @@ const agentInputNormalizationGuidance = (annotation) => {
|
|
|
685
734
|
switch (annotation.kind) {
|
|
686
735
|
case "boolean": return "Use a JSON boolean; common unambiguous yes/no words are normalized.";
|
|
687
736
|
case "country": return "An ISO 3166-1 alpha-3 or alpha-2 code, or the country's name, is read.";
|
|
737
|
+
case "currency": return "Use a three-letter currency code; case and surrounding whitespace are normalized.";
|
|
738
|
+
case "data-box": return "Use the seven-character data box ID; surrounding whitespace is trimmed.";
|
|
739
|
+
case "email": return "Use an email address; surrounding whitespace is trimmed.";
|
|
688
740
|
case "date": return annotation.bound === void 0 ? "Use ISO YYYY-MM-DD; unambiguous localized calendar dates are normalized." : `Use ISO YYYY-MM-DD; a bare year or year-month reads as its ${annotation.bound === "start" ? "first" : "last"} day.`;
|
|
689
741
|
case "date-format": return "Use an object with a BCP-47 locale, such as {\"locale\":\"en-GB\",\"style\":\"short\"}; an unambiguous combined string such as \"en-GB-short\" is normalized.";
|
|
690
742
|
case "eli": return "A short, prefix-less or reordered ELI is read as the canonical one.";
|
|
@@ -692,6 +744,7 @@ const agentInputNormalizationGuidance = (annotation) => {
|
|
|
692
744
|
case "filter": return "Omit it to search without this filter; a placeholder such as \"all\" or \"-\" reads as no filter.";
|
|
693
745
|
case "locale": return "Use a BCP-47 language tag, for example \"cs\" or \"en-GB\".";
|
|
694
746
|
case "number": return annotation.range === "clamp" ? "Use a JSON number; a value outside the range is clamped to it." : "Use a JSON number; unambiguous localized numeric notation is normalized.";
|
|
747
|
+
case "slug": return "Use a-z, 0-9 or hyphens; outer whitespace is trimmed.";
|
|
695
748
|
case "string-list": return "Use a JSON array of strings; a single string is read as a one-item list.";
|
|
696
749
|
case "uuid": return "Use an id a previous call returned; a placeholder id such as all zeros reads as no value.";
|
|
697
750
|
default: return panic("Unhandled agent input normalization kind");
|
|
@@ -706,12 +759,12 @@ const agentInputNormalizationMetadata = (annotation, description) => {
|
|
|
706
759
|
description: authored === void 0 || authored.length === 0 ? guidance : `${authored}${/[.!?]$/u.test(authored) ? " " : ". "}${guidance}`
|
|
707
760
|
};
|
|
708
761
|
};
|
|
709
|
-
const isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
710
|
-
const isUnknownArray = (value) => Array.isArray(value);
|
|
762
|
+
const isRecord$1 = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
763
|
+
const isUnknownArray$1 = (value) => Array.isArray(value);
|
|
711
764
|
const joinPath = (path, segment) => path.length === 0 ? segment : `${path}.${segment}`;
|
|
712
765
|
const annotationOf = (schema) => {
|
|
713
766
|
const annotation = schema[AGENT_INPUT_NORMALIZATION_KEY];
|
|
714
|
-
if (!isRecord(annotation)) return;
|
|
767
|
+
if (!isRecord$1(annotation)) return;
|
|
715
768
|
const kind = annotation["kind"];
|
|
716
769
|
const normalizedKind = Object.values(AGENT_INPUT_NORMALIZATION_KIND).find((candidate) => candidate === kind);
|
|
717
770
|
if (normalizedKind === void 0) return;
|
|
@@ -739,27 +792,27 @@ const annotationOf = (schema) => {
|
|
|
739
792
|
};
|
|
740
793
|
/** The country half of an annotation, read back from the emitted schema. */
|
|
741
794
|
const countryAnnotationOf = (value) => {
|
|
742
|
-
if (!isRecord(value)) return;
|
|
795
|
+
if (!isRecord$1(value)) return;
|
|
743
796
|
const spelling = value["spelling"];
|
|
744
797
|
if (spelling !== "alpha-3" && spelling !== "alpha-2") return;
|
|
745
798
|
const admitted = value["admitted"];
|
|
746
799
|
const tool = value["tool"];
|
|
747
800
|
return {
|
|
748
801
|
spelling,
|
|
749
|
-
...isUnknownArray(admitted) && admitted.every((code) => typeof code === "string") ? { admitted } : {},
|
|
802
|
+
...isUnknownArray$1(admitted) && admitted.every((code) => typeof code === "string") ? { admitted } : {},
|
|
750
803
|
...typeof tool === "string" ? { tool } : {}
|
|
751
804
|
};
|
|
752
805
|
};
|
|
753
806
|
const stringEnumOf = (schema) => {
|
|
754
807
|
const values = schema["enum"];
|
|
755
|
-
if (isUnknownArray(values) && values.length > 0 && values.every((value) => typeof value === "string")) return values;
|
|
808
|
+
if (isUnknownArray$1(values) && values.length > 0 && values.every((value) => typeof value === "string")) return values;
|
|
756
809
|
if (typeof schema["const"] === "string") return [schema["const"]];
|
|
757
810
|
for (const keyword of ["anyOf", "oneOf"]) {
|
|
758
811
|
const branches = schema[keyword];
|
|
759
|
-
if (!isUnknownArray(branches)) continue;
|
|
812
|
+
if (!isUnknownArray$1(branches)) continue;
|
|
760
813
|
const literals = [];
|
|
761
814
|
for (const branch of branches) {
|
|
762
|
-
if (!isRecord(branch)) return;
|
|
815
|
+
if (!isRecord$1(branch)) return;
|
|
763
816
|
if (branch["type"] === "null") continue;
|
|
764
817
|
if (typeof branch["const"] !== "string") return;
|
|
765
818
|
literals.push(branch["const"]);
|
|
@@ -771,7 +824,7 @@ const inferredAnnotationOf = (schema) => {
|
|
|
771
824
|
const explicit = annotationOf(schema);
|
|
772
825
|
if (explicit !== void 0) return explicit;
|
|
773
826
|
if (schema["format"] === "date") return { kind: AGENT_INPUT_NORMALIZATION_KIND.date };
|
|
774
|
-
const types = isUnknownArray(schema["type"]) ? schema["type"] : [schema["type"]];
|
|
827
|
+
const types = isUnknownArray$1(schema["type"]) ? schema["type"] : [schema["type"]];
|
|
775
828
|
if (schema["format"] === "uuid" && types.includes("string")) return { kind: AGENT_INPUT_NORMALIZATION_KIND.uuid };
|
|
776
829
|
if (types.includes("number") || types.includes("integer")) return { kind: AGENT_INPUT_NORMALIZATION_KIND.number };
|
|
777
830
|
if (types.includes("boolean")) return { kind: AGENT_INPUT_NORMALIZATION_KIND.boolean };
|
|
@@ -809,6 +862,9 @@ const normalizeLeaf = ({ annotation, path, readers, schema, value }) => {
|
|
|
809
862
|
path,
|
|
810
863
|
value
|
|
811
864
|
});
|
|
865
|
+
case "currency": return normalizeCurrencyCode(value);
|
|
866
|
+
case "data-box": return normalizeDataBoxId(value);
|
|
867
|
+
case "email": return normalizeEmailAddress(value);
|
|
812
868
|
case "date": {
|
|
813
869
|
const locales = annotation.locale === void 0 ? void 0 : [annotation.locale];
|
|
814
870
|
return annotation.bound === void 0 ? normalizeDateValue(value, locales === void 0 ? void 0 : { locales }) : normalizeDateBound(value, {
|
|
@@ -826,7 +882,7 @@ const normalizeLeaf = ({ annotation, path, readers, schema, value }) => {
|
|
|
826
882
|
case "locale": return normalizeLocale(value);
|
|
827
883
|
case "number": {
|
|
828
884
|
const locale = annotation.locale;
|
|
829
|
-
const types = isUnknownArray(schema["type"]) ? schema["type"] : [schema["type"]];
|
|
885
|
+
const types = isUnknownArray$1(schema["type"]) ? schema["type"] : [schema["type"]];
|
|
830
886
|
if (annotation.range === "clamp") return normalizeNumberInRange(value, {
|
|
831
887
|
minimum: numericKeyword(schema, "minimum"),
|
|
832
888
|
maximum: numericKeyword(schema, "maximum"),
|
|
@@ -839,6 +895,7 @@ const normalizeLeaf = ({ annotation, path, readers, schema, value }) => {
|
|
|
839
895
|
});
|
|
840
896
|
return normalizeNumber(value, locale === void 0 ? void 0 : { locale });
|
|
841
897
|
}
|
|
898
|
+
case "slug": return normalizeSlug(value);
|
|
842
899
|
case "string-list": return normalizeStringList(value, { split: "never" });
|
|
843
900
|
case "uuid": return normalizeUuid(value);
|
|
844
901
|
default: return panic("Unhandled agent input normalization kind");
|
|
@@ -860,15 +917,15 @@ const placeholderIssue = (path, absent, presence) => {
|
|
|
860
917
|
};
|
|
861
918
|
/** Whether an array schema holds strings, so a lone string can be its one item. */
|
|
862
919
|
const holdsStrings = (items) => {
|
|
863
|
-
if (!isRecord(items)) return false;
|
|
864
|
-
return (isUnknownArray(items["type"]) ? items["type"] : [items["type"]]).includes("string") || stringEnumOf(items) !== void 0;
|
|
920
|
+
if (!isRecord$1(items)) return false;
|
|
921
|
+
return (isUnknownArray$1(items["type"]) ? items["type"] : [items["type"]]).includes("string") || stringEnumOf(items) !== void 0;
|
|
865
922
|
};
|
|
866
923
|
/**
|
|
867
924
|
* Whether a string list's items are constrained tokens (an id, a code, an
|
|
868
925
|
* advertised value), which never carry a comma of their own, so a
|
|
869
926
|
* comma-joined string is a list. Free text is never split.
|
|
870
927
|
*/
|
|
871
|
-
const holdsTokens = (items) => isRecord(items) && (typeof items["format"] === "string" || typeof items["pattern"] === "string" || stringEnumOf(items) !== void 0);
|
|
928
|
+
const holdsTokens = (items) => isRecord$1(items) && (typeof items["format"] === "string" || typeof items["pattern"] === "string" || stringEnumOf(items) !== void 0);
|
|
872
929
|
const MAX_SCHEMA_REFERENCE_DEPTH = 64;
|
|
873
930
|
const valueMatchesType = (type, value) => {
|
|
874
931
|
switch (type) {
|
|
@@ -877,13 +934,13 @@ const valueMatchesType = (type, value) => {
|
|
|
877
934
|
case "integer":
|
|
878
935
|
case "number": return typeof value === "number" || typeof value === "string";
|
|
879
936
|
case "null": return value === null;
|
|
880
|
-
case "object": return isRecord(value);
|
|
937
|
+
case "object": return isRecord$1(value);
|
|
881
938
|
case "string": return typeof value === "string";
|
|
882
939
|
default: return true;
|
|
883
940
|
}
|
|
884
941
|
};
|
|
885
942
|
const walkUnion = ({ branches, value, path, context }) => {
|
|
886
|
-
const results = branches.filter((branch) => isRecord(branch) && valueMatchesType(branch["type"], value)).map((branch) => walkSchema({
|
|
943
|
+
const results = branches.filter((branch) => isRecord$1(branch) && valueMatchesType(branch["type"], value)).map((branch) => walkSchema({
|
|
887
944
|
schema: branch,
|
|
888
945
|
value,
|
|
889
946
|
path,
|
|
@@ -905,7 +962,7 @@ const resolveLocalReference = (reference, rootSchema) => {
|
|
|
905
962
|
if (!reference.startsWith("#/")) return;
|
|
906
963
|
let resolved = rootSchema;
|
|
907
964
|
for (const rawSegment of reference.slice(2).split("/")) {
|
|
908
|
-
if (!isRecord(resolved)) return;
|
|
965
|
+
if (!isRecord$1(resolved)) return;
|
|
909
966
|
const segment = rawSegment.replaceAll("~1", "/").replaceAll("~0", "~");
|
|
910
967
|
resolved = resolved[segment];
|
|
911
968
|
}
|
|
@@ -958,7 +1015,7 @@ const walkIntersection = ({ schema, branches, value, path, context }) => {
|
|
|
958
1015
|
} : { status: "not-applicable" };
|
|
959
1016
|
};
|
|
960
1017
|
const walkSchema = ({ schema, value, path, context }) => {
|
|
961
|
-
if (!isRecord(schema)) return { status: "not-applicable" };
|
|
1018
|
+
if (!isRecord$1(schema)) return { status: "not-applicable" };
|
|
962
1019
|
const reference = schema["$ref"];
|
|
963
1020
|
if (typeof reference === "string") {
|
|
964
1021
|
if (context.remainingReferenceDepth === 0) return { status: "not-applicable" };
|
|
@@ -974,7 +1031,7 @@ const walkSchema = ({ schema, value, path, context }) => {
|
|
|
974
1031
|
});
|
|
975
1032
|
}
|
|
976
1033
|
const intersections = schema["allOf"];
|
|
977
|
-
if (isUnknownArray(intersections)) return walkIntersection({
|
|
1034
|
+
if (isUnknownArray$1(intersections)) return walkIntersection({
|
|
978
1035
|
schema,
|
|
979
1036
|
branches: intersections,
|
|
980
1037
|
value,
|
|
@@ -984,7 +1041,7 @@ const walkSchema = ({ schema, value, path, context }) => {
|
|
|
984
1041
|
const explicitAnnotation = annotationOf(schema);
|
|
985
1042
|
if (explicitAnnotation === void 0) for (const keyword of ["anyOf", "oneOf"]) {
|
|
986
1043
|
const branches = schema[keyword];
|
|
987
|
-
if (isUnknownArray(branches)) return walkUnion({
|
|
1044
|
+
if (isUnknownArray$1(branches)) return walkUnion({
|
|
988
1045
|
branches,
|
|
989
1046
|
value,
|
|
990
1047
|
path,
|
|
@@ -992,7 +1049,7 @@ const walkSchema = ({ schema, value, path, context }) => {
|
|
|
992
1049
|
});
|
|
993
1050
|
}
|
|
994
1051
|
const annotation = explicitAnnotation ?? inferredAnnotationOf(schema);
|
|
995
|
-
const types = isUnknownArray(schema["type"]) ? schema["type"] : [schema["type"]];
|
|
1052
|
+
const types = isUnknownArray$1(schema["type"]) ? schema["type"] : [schema["type"]];
|
|
996
1053
|
if (annotation !== void 0 && !(value === null && types.includes("null"))) {
|
|
997
1054
|
const normalized = normalizeLeaf({
|
|
998
1055
|
annotation,
|
|
@@ -1028,7 +1085,7 @@ const walkSchema = ({ schema, value, path, context }) => {
|
|
|
1028
1085
|
}
|
|
1029
1086
|
for (const keyword of ["anyOf", "oneOf"]) {
|
|
1030
1087
|
const branches = schema[keyword];
|
|
1031
|
-
if (isUnknownArray(branches)) return walkUnion({
|
|
1088
|
+
if (isUnknownArray$1(branches)) return walkUnion({
|
|
1032
1089
|
branches,
|
|
1033
1090
|
value,
|
|
1034
1091
|
path,
|
|
@@ -1037,7 +1094,7 @@ const walkSchema = ({ schema, value, path, context }) => {
|
|
|
1037
1094
|
}
|
|
1038
1095
|
const properties = schema["properties"];
|
|
1039
1096
|
const patternProperties = schema["patternProperties"];
|
|
1040
|
-
if (isRecord(value) && (isRecord(properties) || isRecord(patternProperties))) return walkObject({
|
|
1097
|
+
if (isRecord$1(value) && (isRecord$1(properties) || isRecord$1(patternProperties))) return walkObject({
|
|
1041
1098
|
schema,
|
|
1042
1099
|
value,
|
|
1043
1100
|
path,
|
|
@@ -1056,12 +1113,12 @@ const childSchemasOf = (schema, key) => {
|
|
|
1056
1113
|
const properties = schema["properties"];
|
|
1057
1114
|
const patternProperties = schema["patternProperties"];
|
|
1058
1115
|
const childSchemas = [];
|
|
1059
|
-
if (isRecord(properties) && Object.hasOwn(properties, key)) childSchemas.push(properties[key]);
|
|
1060
|
-
if (isRecord(patternProperties)) {
|
|
1116
|
+
if (isRecord$1(properties) && Object.hasOwn(properties, key)) childSchemas.push(properties[key]);
|
|
1117
|
+
if (isRecord$1(patternProperties)) {
|
|
1061
1118
|
for (const [pattern, childSchema] of Object.entries(patternProperties)) if (new RegExp(pattern, "u").test(key)) childSchemas.push(childSchema);
|
|
1062
1119
|
}
|
|
1063
1120
|
const additionalProperties = schema["additionalProperties"];
|
|
1064
|
-
if (childSchemas.length === 0 && isRecord(additionalProperties)) childSchemas.push(additionalProperties);
|
|
1121
|
+
if (childSchemas.length === 0 && isRecord$1(additionalProperties)) childSchemas.push(additionalProperties);
|
|
1065
1122
|
return childSchemas;
|
|
1066
1123
|
};
|
|
1067
1124
|
const walkObject = ({ schema, value, path, context }) => {
|
|
@@ -1069,7 +1126,7 @@ const walkObject = ({ schema, value, path, context }) => {
|
|
|
1069
1126
|
const notes = [];
|
|
1070
1127
|
const issues = [];
|
|
1071
1128
|
const requiredKeys = schema["required"];
|
|
1072
|
-
const required = new Set(isUnknownArray(requiredKeys) ? requiredKeys : []);
|
|
1129
|
+
const required = new Set(isUnknownArray$1(requiredKeys) ? requiredKeys : []);
|
|
1073
1130
|
for (const [key, entry] of Object.entries(value)) {
|
|
1074
1131
|
let reading = {
|
|
1075
1132
|
status: "value",
|
|
@@ -1135,7 +1192,7 @@ const walkList = ({ items, value, path, context }) => {
|
|
|
1135
1192
|
list = read.value;
|
|
1136
1193
|
if (read.note !== void 0) notes.push(read.note);
|
|
1137
1194
|
}
|
|
1138
|
-
if (!isUnknownArray(list)) return { status: "not-applicable" };
|
|
1195
|
+
if (!isUnknownArray$1(list)) return { status: "not-applicable" };
|
|
1139
1196
|
const output = [];
|
|
1140
1197
|
const issues = [];
|
|
1141
1198
|
for (const [index, entry] of list.entries()) {
|
|
@@ -1197,4 +1254,351 @@ const normalizeAgentInput = ({ path = "", placeholders = "absent", readers = {},
|
|
|
1197
1254
|
};
|
|
1198
1255
|
};
|
|
1199
1256
|
//#endregion
|
|
1200
|
-
|
|
1257
|
+
//#region src/null-optionals.ts
|
|
1258
|
+
const isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
1259
|
+
const isUnknownArray = (value) => Array.isArray(value);
|
|
1260
|
+
const sameJsonValue = (left, right) => {
|
|
1261
|
+
if (Object.is(left, right)) return true;
|
|
1262
|
+
if (isUnknownArray(left) && isUnknownArray(right)) return left.length === right.length && left.every((value, index) => sameJsonValue(value, right.at(index)));
|
|
1263
|
+
if (!isRecord(left) || !isRecord(right) || Object.getPrototypeOf(left) !== Object.getPrototypeOf(right)) return false;
|
|
1264
|
+
const keys = Object.keys(left);
|
|
1265
|
+
return keys.length === Object.keys(right).length && keys.every((key) => Object.hasOwn(right, key) && sameJsonValue(left[key], right[key]));
|
|
1266
|
+
};
|
|
1267
|
+
const CONSTRAINING_KEYWORDS = [
|
|
1268
|
+
"type",
|
|
1269
|
+
"anyOf",
|
|
1270
|
+
"oneOf",
|
|
1271
|
+
"allOf",
|
|
1272
|
+
"enum",
|
|
1273
|
+
"const",
|
|
1274
|
+
"$ref"
|
|
1275
|
+
];
|
|
1276
|
+
const admitsNull = (schema) => {
|
|
1277
|
+
if (!isRecord(schema)) return true;
|
|
1278
|
+
if (schema["nullable"] === true) return true;
|
|
1279
|
+
const type = schema["type"];
|
|
1280
|
+
if (type === "null" || isUnknownArray(type) && type.includes("null")) return true;
|
|
1281
|
+
for (const keyword of ["anyOf", "oneOf"]) {
|
|
1282
|
+
const branches = schema[keyword];
|
|
1283
|
+
if (isUnknownArray(branches) && branches.some(admitsNull)) return true;
|
|
1284
|
+
}
|
|
1285
|
+
return CONSTRAINING_KEYWORDS.every((keyword) => !(keyword in schema));
|
|
1286
|
+
};
|
|
1287
|
+
const UNION_KEYWORDS = ["anyOf", "oneOf"];
|
|
1288
|
+
/**
|
|
1289
|
+
* Whether `text` matches a schema's `pattern`, or `undefined` when the pattern
|
|
1290
|
+
* does not compile as a Unicode expression: a third-party tool schema may
|
|
1291
|
+
* carry one (`^[a-z\_]+$`), and it must not fail the whole input.
|
|
1292
|
+
*/
|
|
1293
|
+
const matchesPattern = (pattern, text) => {
|
|
1294
|
+
const compiled = Result.try(() => new RegExp(pattern, "u"));
|
|
1295
|
+
return Result.isOk(compiled) ? compiled.value.test(text) : void 0;
|
|
1296
|
+
};
|
|
1297
|
+
/**
|
|
1298
|
+
* Whether a union branch could be the one `value` was written for: no `const`
|
|
1299
|
+
* property of the branch (a discriminator) disagrees with the value. A branch
|
|
1300
|
+
* with no discriminator always could.
|
|
1301
|
+
*/
|
|
1302
|
+
const branchCouldMatch = (branch, value) => {
|
|
1303
|
+
const properties = branch["properties"];
|
|
1304
|
+
if (!isRecord(properties)) return true;
|
|
1305
|
+
return Object.entries(properties).every(([name, property]) => !isRecord(property) || !("const" in property) || !Object.hasOwn(value, name) || sameJsonValue(property["const"], value[name]));
|
|
1306
|
+
};
|
|
1307
|
+
const jsonTypeOf = (value) => {
|
|
1308
|
+
if (value === null) return "null";
|
|
1309
|
+
if (isUnknownArray(value)) return "array";
|
|
1310
|
+
if (typeof value === "number") return Number.isInteger(value) ? "integer" : "number";
|
|
1311
|
+
return typeof value;
|
|
1312
|
+
};
|
|
1313
|
+
const typeTakes = (type, value) => {
|
|
1314
|
+
if (type === void 0) return true;
|
|
1315
|
+
const types = isUnknownArray(type) ? type : [type];
|
|
1316
|
+
const valueType = jsonTypeOf(value);
|
|
1317
|
+
return types.includes(valueType) || valueType === "integer" && types.includes("number");
|
|
1318
|
+
};
|
|
1319
|
+
/**
|
|
1320
|
+
* Whether `value` has a shape `schema` takes: its types, enums, constants,
|
|
1321
|
+
* required and closed object fields, read through unions, objects and arrays.
|
|
1322
|
+
* A placeholder (null, "") fits anywhere, since it is what is being judged,
|
|
1323
|
+
* and string constraints and references are left to validation.
|
|
1324
|
+
*/
|
|
1325
|
+
const shapeTakes = (schema, value) => {
|
|
1326
|
+
if (!isRecord(schema) || value === null || value === "") return true;
|
|
1327
|
+
for (const keyword of UNION_KEYWORDS) {
|
|
1328
|
+
const branches = schema[keyword];
|
|
1329
|
+
if (isUnknownArray(branches) && !branches.some((branch) => shapeTakes(branch, value))) return false;
|
|
1330
|
+
}
|
|
1331
|
+
const enumValues = schema["enum"];
|
|
1332
|
+
if (isUnknownArray(enumValues)) return enumValues.some((allowed) => sameJsonValue(allowed, value));
|
|
1333
|
+
if ("const" in schema) return sameJsonValue(schema["const"], value);
|
|
1334
|
+
if (!typeTakes(schema["type"], value)) return false;
|
|
1335
|
+
if (isUnknownArray(value)) return arrayShapeTakes(schema, value);
|
|
1336
|
+
return !isRecord(value) || objectShapeTakes(schema, value);
|
|
1337
|
+
};
|
|
1338
|
+
const arrayShapeTakes = (schema, value) => {
|
|
1339
|
+
const { maxItems, minItems } = schema;
|
|
1340
|
+
if (typeof minItems === "number" && value.length < minItems) return false;
|
|
1341
|
+
if (typeof maxItems === "number" && value.length > maxItems) return false;
|
|
1342
|
+
const items = schema["items"];
|
|
1343
|
+
return !isRecord(items) || value.every((entry) => shapeTakes(items, entry));
|
|
1344
|
+
};
|
|
1345
|
+
/**
|
|
1346
|
+
* Whether a field the object does not declare by name fits it: every
|
|
1347
|
+
* `patternProperties` schema its name matches applies, and a name matching
|
|
1348
|
+
* none falls to `additionalProperties`. A pattern that does not compile
|
|
1349
|
+
* decides nothing.
|
|
1350
|
+
*/
|
|
1351
|
+
const extraFieldTakes = (schema, key, entry) => {
|
|
1352
|
+
const patternProperties = schema["patternProperties"];
|
|
1353
|
+
let matched = false;
|
|
1354
|
+
if (isRecord(patternProperties)) for (const [pattern, childSchema] of Object.entries(patternProperties)) {
|
|
1355
|
+
const matches = matchesPattern(pattern, key);
|
|
1356
|
+
if (matches === void 0) return true;
|
|
1357
|
+
if (matches) {
|
|
1358
|
+
matched = true;
|
|
1359
|
+
if (!shapeTakes(childSchema, entry)) return false;
|
|
1360
|
+
}
|
|
1361
|
+
}
|
|
1362
|
+
if (matched) return true;
|
|
1363
|
+
const additionalProperties = schema["additionalProperties"];
|
|
1364
|
+
if (additionalProperties === false) return entry === null || entry === "";
|
|
1365
|
+
return shapeTakes(additionalProperties, entry);
|
|
1366
|
+
};
|
|
1367
|
+
const objectShapeTakes = (schema, value) => {
|
|
1368
|
+
const required = schema["required"];
|
|
1369
|
+
if (isUnknownArray(required) && !required.every((name) => typeof name !== "string" || Object.hasOwn(value, name))) return false;
|
|
1370
|
+
const properties = isRecord(schema["properties"]) ? schema["properties"] : {};
|
|
1371
|
+
return Object.entries(value).every(([key, entry]) => Object.hasOwn(properties, key) ? shapeTakes(properties[key], entry) : extraFieldTakes(schema, key, entry));
|
|
1372
|
+
};
|
|
1373
|
+
/**
|
|
1374
|
+
* A union node declares nothing itself: its members do. Without reading them,
|
|
1375
|
+
* an optional null inside a discriminated member (an array of `mode` variants)
|
|
1376
|
+
* reached validation and was refused. Of the members no discriminator rules
|
|
1377
|
+
* out, those whose shape takes the value are the ones it was written for; a
|
|
1378
|
+
* member it cannot fit must not keep a null the fitting one refuses. When
|
|
1379
|
+
* none fits (a lenient value, coerced later), every such member still counts.
|
|
1380
|
+
*/
|
|
1381
|
+
const matchedBranches = /* @__PURE__ */ new WeakMap();
|
|
1382
|
+
const matchingUnionBranches = (schema, value) => {
|
|
1383
|
+
const bySchema = matchedBranches.get(schema) ?? /* @__PURE__ */ new WeakMap();
|
|
1384
|
+
matchedBranches.set(schema, bySchema);
|
|
1385
|
+
const known = bySchema.get(value);
|
|
1386
|
+
if (known !== void 0) return known;
|
|
1387
|
+
const matching = [];
|
|
1388
|
+
for (const keyword of UNION_KEYWORDS) {
|
|
1389
|
+
const branches = schema[keyword];
|
|
1390
|
+
if (!isUnknownArray(branches)) continue;
|
|
1391
|
+
for (const branch of branches) if (isRecord(branch) && branchCouldMatch(branch, value)) matching.push(branch);
|
|
1392
|
+
}
|
|
1393
|
+
const fitting = matching.filter((branch) => shapeTakes(branch, value));
|
|
1394
|
+
const matched = fitting.length > 0 ? fitting : matching;
|
|
1395
|
+
bySchema.set(value, matched);
|
|
1396
|
+
return matched;
|
|
1397
|
+
};
|
|
1398
|
+
const requiredNamesOf = (schema, value) => {
|
|
1399
|
+
const required = schema["required"];
|
|
1400
|
+
const names = new Set(isUnknownArray(required) ? required : []);
|
|
1401
|
+
for (const branch of matchingUnionBranches(schema, value)) for (const name of requiredNamesOf(branch, value)) names.add(name);
|
|
1402
|
+
return names;
|
|
1403
|
+
};
|
|
1404
|
+
/** The schemas a node itself, not its union branches, gives `key`. */
|
|
1405
|
+
const ownChildSchemas = (schema, key) => {
|
|
1406
|
+
const children = [];
|
|
1407
|
+
const properties = schema["properties"];
|
|
1408
|
+
if (isRecord(properties) && Object.hasOwn(properties, key)) children.push(properties[key]);
|
|
1409
|
+
const patternProperties = schema["patternProperties"];
|
|
1410
|
+
if (isRecord(patternProperties)) {
|
|
1411
|
+
for (const [pattern, childSchema] of Object.entries(patternProperties)) if (matchesPattern(pattern, key) === true) children.push(childSchema);
|
|
1412
|
+
}
|
|
1413
|
+
return children;
|
|
1414
|
+
};
|
|
1415
|
+
const objectChildSchemas = (schema, key, value) => {
|
|
1416
|
+
const children = [];
|
|
1417
|
+
for (const branch of matchingUnionBranches(schema, value)) children.push(...objectChildSchemas(branch, key, value));
|
|
1418
|
+
children.push(...ownChildSchemas(schema, key));
|
|
1419
|
+
const additionalProperties = schema["additionalProperties"];
|
|
1420
|
+
if (children.length === 0 && isRecord(additionalProperties)) children.push(additionalProperties);
|
|
1421
|
+
return children;
|
|
1422
|
+
};
|
|
1423
|
+
/**
|
|
1424
|
+
* Whether the node refuses `entry` at `key`. Every schema the node itself
|
|
1425
|
+
* gives the key applies, so one refusing it is enough; union branches are
|
|
1426
|
+
* alternatives, so every branch that declares the key must refuse it, or a
|
|
1427
|
+
* value valid under one branch would lose a field another branch refuses.
|
|
1428
|
+
*/
|
|
1429
|
+
const refusesPlaceholder = (schema, site) => {
|
|
1430
|
+
const { entry, isAbsent, key, value } = site;
|
|
1431
|
+
const branches = matchingUnionBranches(schema, value).filter((branch) => objectChildSchemas(branch, key, value).length > 0);
|
|
1432
|
+
const own = ownChildSchemas(schema, key);
|
|
1433
|
+
const additionalProperties = schema["additionalProperties"];
|
|
1434
|
+
if (own.length === 0 && branches.length === 0 && isRecord(additionalProperties)) own.push(additionalProperties);
|
|
1435
|
+
if (own.some((childSchema) => isAbsent(entry, childSchema))) return true;
|
|
1436
|
+
return branches.length > 0 && branches.every((branch) => refusesPlaceholder(branch, site));
|
|
1437
|
+
};
|
|
1438
|
+
/** The item schemas a union node's array members, nested ones too, declare. */
|
|
1439
|
+
const unionItemSchemas = (schema, value) => {
|
|
1440
|
+
const members = UNION_KEYWORDS.flatMap((keyword) => {
|
|
1441
|
+
const branches = schema[keyword];
|
|
1442
|
+
return (isUnknownArray(branches) ? branches : []).filter(isRecord);
|
|
1443
|
+
});
|
|
1444
|
+
const fitting = members.filter((member) => shapeTakes(member, value));
|
|
1445
|
+
return (fitting.length > 0 ? fitting : members).flatMap((member) => member["items"] === void 0 ? unionItemSchemas(member, value) : [member["items"]]);
|
|
1446
|
+
};
|
|
1447
|
+
const omitAbsentPlaceholders = (schema, value, isAbsent) => {
|
|
1448
|
+
if (!isRecord(schema)) return value;
|
|
1449
|
+
const items = schema["items"];
|
|
1450
|
+
if (items !== void 0 && isUnknownArray(value)) return value.map((entry) => omitAbsentPlaceholders(items, entry, isAbsent));
|
|
1451
|
+
if (isUnknownArray(value)) {
|
|
1452
|
+
const itemSchemas = unionItemSchemas(schema, value);
|
|
1453
|
+
if (itemSchemas.length === 0) return value;
|
|
1454
|
+
const itemSchema = itemSchemas.length === 1 ? itemSchemas[0] : { anyOf: itemSchemas };
|
|
1455
|
+
return value.map((entry) => omitAbsentPlaceholders(itemSchema, entry, isAbsent));
|
|
1456
|
+
}
|
|
1457
|
+
if (!isRecord(value)) return value;
|
|
1458
|
+
const requiredNames = requiredNamesOf(schema, value);
|
|
1459
|
+
return Object.fromEntries(Object.entries(value).flatMap(([key, entry]) => {
|
|
1460
|
+
const childSchemas = objectChildSchemas(schema, key, value);
|
|
1461
|
+
if (childSchemas.length === 0) return [[key, entry]];
|
|
1462
|
+
if (!requiredNames.has(key) && refusesPlaceholder(schema, {
|
|
1463
|
+
entry,
|
|
1464
|
+
isAbsent,
|
|
1465
|
+
key,
|
|
1466
|
+
value
|
|
1467
|
+
})) return [];
|
|
1468
|
+
let current = entry;
|
|
1469
|
+
for (const childSchema of childSchemas) current = omitAbsentPlaceholders(childSchema, current, isAbsent);
|
|
1470
|
+
return [[key, current]];
|
|
1471
|
+
}));
|
|
1472
|
+
};
|
|
1473
|
+
const isRejectedNull = (value, schema) => value === null && !admitsNull(schema);
|
|
1474
|
+
/**
|
|
1475
|
+
* JSON Schema string formats that no empty string satisfies. An unlisted
|
|
1476
|
+
* format is an annotation a validator may ignore, so it decides nothing.
|
|
1477
|
+
*/
|
|
1478
|
+
const EMPTY_REJECTING_FORMATS = /* @__PURE__ */ new Set([
|
|
1479
|
+
"date",
|
|
1480
|
+
"date-time",
|
|
1481
|
+
"time",
|
|
1482
|
+
"duration",
|
|
1483
|
+
"email",
|
|
1484
|
+
"hostname",
|
|
1485
|
+
"ipv4",
|
|
1486
|
+
"ipv6",
|
|
1487
|
+
"uri",
|
|
1488
|
+
"uuid"
|
|
1489
|
+
]);
|
|
1490
|
+
/**
|
|
1491
|
+
* Whether a schema refuses the empty string: a declared type other than
|
|
1492
|
+
* string, a string with a minimum length, a format "" cannot satisfy or a
|
|
1493
|
+
* pattern "" does not match, or an enum or constant without "". A union
|
|
1494
|
+
* refuses it only when every branch does. A schema that declares no type
|
|
1495
|
+
* accepts it.
|
|
1496
|
+
*/
|
|
1497
|
+
const rejectsEmptyString = (schema) => {
|
|
1498
|
+
if (!isRecord(schema)) return false;
|
|
1499
|
+
for (const keyword of UNION_KEYWORDS) {
|
|
1500
|
+
const branches = schema[keyword];
|
|
1501
|
+
if (isUnknownArray(branches) && branches.length > 0) return branches.every(rejectsEmptyString);
|
|
1502
|
+
}
|
|
1503
|
+
const enumValues = schema["enum"];
|
|
1504
|
+
if (isUnknownArray(enumValues)) return !enumValues.includes("");
|
|
1505
|
+
if ("const" in schema) return schema["const"] !== "";
|
|
1506
|
+
const type = schema["type"];
|
|
1507
|
+
if (type === void 0) return false;
|
|
1508
|
+
if (!(type === "string" || isUnknownArray(type) && type.includes("string"))) return true;
|
|
1509
|
+
const minLength = schema["minLength"];
|
|
1510
|
+
if (typeof minLength === "number" && minLength > 0) return true;
|
|
1511
|
+
const format = schema["format"];
|
|
1512
|
+
if (typeof format === "string" && EMPTY_REJECTING_FORMATS.has(format)) return true;
|
|
1513
|
+
const pattern = schema["pattern"];
|
|
1514
|
+
return typeof pattern === "string" && matchesPattern(pattern, "") === false;
|
|
1515
|
+
};
|
|
1516
|
+
const isRejectedPlaceholder = (value, schema) => isRejectedNull(value, schema) || value === "" && rejectsEmptyString(schema);
|
|
1517
|
+
/**
|
|
1518
|
+
* Apply the common optional-null rule before any agent-value coercion. The
|
|
1519
|
+
* decision comes from the same schema validation will use: nullable values keep
|
|
1520
|
+
* null, required values still fail, and optional non-null values read null as
|
|
1521
|
+
* omission at every declared object level.
|
|
1522
|
+
*/
|
|
1523
|
+
const withNullOptionalsOmitted = (schema, value) => omitAbsentPlaceholders(schema, value, isRejectedNull);
|
|
1524
|
+
/**
|
|
1525
|
+
* The same rule for a model's tool input, which also fills an optional string
|
|
1526
|
+
* it is not setting with "": that reads as omitted too, but only where the
|
|
1527
|
+
* property's own schema refuses "". Where "" is a value the property
|
|
1528
|
+
* accepts, it stays exactly as sent. This is the placeholder rule the MCP
|
|
1529
|
+
* input schemas apply (`nullAsAbsent`), read off JSON Schema.
|
|
1530
|
+
*/
|
|
1531
|
+
const withModelPlaceholdersOmitted = (schema, value) => omitAbsentPlaceholders(schema, value, isRejectedPlaceholder);
|
|
1532
|
+
const NULL_SCHEMA = { type: "null" };
|
|
1533
|
+
/** Keywords past which a node is more than a bare `type`. */
|
|
1534
|
+
const BEYOND_TYPE_KEYWORDS = [
|
|
1535
|
+
"anyOf",
|
|
1536
|
+
"oneOf",
|
|
1537
|
+
"allOf",
|
|
1538
|
+
"enum",
|
|
1539
|
+
"const",
|
|
1540
|
+
"$ref"
|
|
1541
|
+
];
|
|
1542
|
+
/** `schema`, also admitting `null`. */
|
|
1543
|
+
const admittingNull = (schema) => {
|
|
1544
|
+
if (!isRecord(schema) || admitsNull(schema)) return schema;
|
|
1545
|
+
const type = schema["type"];
|
|
1546
|
+
if (BEYOND_TYPE_KEYWORDS.every((keyword) => !(keyword in schema))) {
|
|
1547
|
+
if (typeof type === "string") return {
|
|
1548
|
+
...schema,
|
|
1549
|
+
type: [type, "null"]
|
|
1550
|
+
};
|
|
1551
|
+
if (isUnknownArray(type)) return {
|
|
1552
|
+
...schema,
|
|
1553
|
+
type: [...type, "null"]
|
|
1554
|
+
};
|
|
1555
|
+
}
|
|
1556
|
+
return { anyOf: [schema, NULL_SCHEMA] };
|
|
1557
|
+
};
|
|
1558
|
+
/** The names a node requires, its union branches' included. */
|
|
1559
|
+
const requiredAnywhere = (schema) => {
|
|
1560
|
+
const required = schema["required"];
|
|
1561
|
+
const names = new Set(isUnknownArray(required) ? required : []);
|
|
1562
|
+
for (const keyword of UNION_KEYWORDS) {
|
|
1563
|
+
const branches = schema[keyword];
|
|
1564
|
+
if (!isUnknownArray(branches)) continue;
|
|
1565
|
+
for (const branch of branches) if (isRecord(branch)) for (const name of requiredAnywhere(branch)) names.add(name);
|
|
1566
|
+
}
|
|
1567
|
+
return names;
|
|
1568
|
+
};
|
|
1569
|
+
const mapEntries = (record, map) => Object.fromEntries(Object.entries(record).map(([name, entry]) => [name, map(name, entry)]));
|
|
1570
|
+
const NO_NAMES = /* @__PURE__ */ new Set();
|
|
1571
|
+
/** A union branch's object is read with its parent's required names too. */
|
|
1572
|
+
const widenOptionals = (schema, inheritedRequired) => {
|
|
1573
|
+
if (!isRecord(schema)) return schema;
|
|
1574
|
+
const required = requiredAnywhere(schema);
|
|
1575
|
+
for (const name of inheritedRequired) required.add(name);
|
|
1576
|
+
const properties = schema["properties"];
|
|
1577
|
+
const patternProperties = schema["patternProperties"];
|
|
1578
|
+
return {
|
|
1579
|
+
...schema,
|
|
1580
|
+
...isRecord(properties) ? { properties: mapEntries(properties, (name, property) => {
|
|
1581
|
+
const inner = widenOptionals(property, NO_NAMES);
|
|
1582
|
+
return required.has(name) ? inner : admittingNull(inner);
|
|
1583
|
+
}) } : {},
|
|
1584
|
+
...isRecord(patternProperties) ? { patternProperties: mapEntries(patternProperties, (_name, entry) => widenOptionals(entry, NO_NAMES)) } : {},
|
|
1585
|
+
...Object.fromEntries(["additionalProperties", "items"].flatMap((keyword) => {
|
|
1586
|
+
const child = schema[keyword];
|
|
1587
|
+
return isRecord(child) ? [[keyword, widenOptionals(child, NO_NAMES)]] : [];
|
|
1588
|
+
})),
|
|
1589
|
+
...Object.fromEntries(UNION_KEYWORDS.flatMap((keyword) => {
|
|
1590
|
+
const branches = schema[keyword];
|
|
1591
|
+
return isUnknownArray(branches) ? [[keyword, branches.map((branch) => widenOptionals(branch, required))]] : [];
|
|
1592
|
+
}))
|
|
1593
|
+
};
|
|
1594
|
+
};
|
|
1595
|
+
/**
|
|
1596
|
+
* `schema` with every optional property also admitting `null`, at every level
|
|
1597
|
+
* `withModelPlaceholdersOmitted` reads: the spelling a model can use for "not
|
|
1598
|
+
* set" on a route that treats every declared field as one to fill. Read
|
|
1599
|
+
* against the original schema, `withModelPlaceholdersOmitted` drops exactly
|
|
1600
|
+
* those nulls again, so the input a tool receives keeps its declared shape.
|
|
1601
|
+
*/
|
|
1602
|
+
const withOptionalsNullable = (schema) => widenOptionals(schema, NO_NAMES);
|
|
1603
|
+
//#endregion
|
|
1604
|
+
export { ABSENT_PLACEHOLDERS, AGENT_INPUT_NORMALIZATION_KEY, AGENT_INPUT_NORMALIZATION_KIND, COUNTRY_INPUT_MAX_CHARS, DATA_BOX_ID_PATTERN, DATE_FORMAT_SPEC_HINT, DATE_VALUE_HINT, EMAIL_ADDRESS_PATTERN, SENTINEL_UUIDS, agentInputNormalization, agentInputNormalizationGuidance, agentInputNormalizationMetadata, askForFix, askSentence, countryCodeIn, isAbsentPlaceholder, isPlausibleLocale, isSentinelUuid, normalizeAgentInput, normalizeBoolean, normalizeCountry, normalizeDateBound, normalizeDateFormatSpec, normalizeDateValue, normalizeEli, normalizeEnumValue, normalizeLocale, normalizeNumber, normalizeNumberInRange, normalizeStringList, normalizeUuid, normalizeVocabularyValue, readAsAbsent, withModelPlaceholdersOmitted, withNullOptionalsOmitted, withOptionalsNullable };
|
package/dist/numeric-token.d.ts
CHANGED
|
@@ -15,4 +15,5 @@ export type NumericTokenOptions = {
|
|
|
15
15
|
export declare const parseIntegerToken: (input: unknown, options?: NumericTokenOptions) => Result<number, InvalidNumericTokenError>;
|
|
16
16
|
/** Grammar: `-?[0-9]+(\.[0-9]+)?`; no grouping, exponent or implicit whitespace. */
|
|
17
17
|
export declare const parseDecimalToken: (input: unknown, options?: NumericTokenOptions) => Result<number, InvalidNumericTokenError>;
|
|
18
|
-
//#endregion
|
|
18
|
+
//#endregion
|
|
19
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stll/agent-input",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "Lenient readers for values agents write: dates, date bounds, date-format specs, numbers, booleans, locales, countries, enum and vocabulary values, ids, ELIs, string lists",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"agent",
|
|
@@ -71,7 +71,7 @@
|
|
|
71
71
|
"@stll/country-codes": "^0.2.0",
|
|
72
72
|
"@stll/fuzzy-search": "1.4.0",
|
|
73
73
|
"@stll/template-conditions": "^0.6.4",
|
|
74
|
-
"@stll/text-normalize": "^0.5.
|
|
74
|
+
"@stll/text-normalize": "^0.5.2",
|
|
75
75
|
"@stll/time": "0.4.1",
|
|
76
76
|
"better-result": "3.0.1"
|
|
77
77
|
},
|
|
@@ -80,7 +80,7 @@
|
|
|
80
80
|
"@stll/typescript-config": "0.0.0",
|
|
81
81
|
"bun-types": "1.4.4",
|
|
82
82
|
"fast-check": "^4.10.2",
|
|
83
|
-
"tsdown": "0.23.
|
|
83
|
+
"tsdown": "0.23.1",
|
|
84
84
|
"tsrs": "0.18.0-ts7.1.0-dev.20260929"
|
|
85
85
|
},
|
|
86
86
|
"main": "./dist/index.js",
|