@alcyone-labs/arg-parser 2.4.2 → 2.5.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/index.mjs CHANGED
@@ -1070,6 +1070,11 @@ const zodFlagSchema = z.object({
1070
1070
  "Must be a custom parser function"
1071
1071
  ),
1072
1072
  // Custom parser function (value: string) => any | Promise<any>
1073
+ z.custom(
1074
+ (val) => val && typeof val === "object" && val._def,
1075
+ "Must be a Zod schema"
1076
+ ),
1077
+ // Zod schema for structured JSON validation
1073
1078
  z.string().refine(
1074
1079
  // String literal types
1075
1080
  (value) => ["boolean", "string", "number", "array", "object"].includes(
@@ -1080,7 +1085,7 @@ const zodFlagSchema = z.object({
1080
1085
  }
1081
1086
  )
1082
1087
  ]).default("string").describe(
1083
- "Expected data type (constructor or string literal) or a custom parser function. Defaults to 'string'."
1088
+ "Expected data type (constructor, string literal, custom parser function, or Zod schema). Defaults to 'string'."
1084
1089
  ),
1085
1090
  mandatory: z.union([
1086
1091
  z.boolean(),
@@ -1112,6 +1117,9 @@ const zodFlagSchema = z.object({
1112
1117
  return newObj;
1113
1118
  });
1114
1119
  function getJsonSchemaTypeFromFlag(flagType) {
1120
+ if (flagType && typeof flagType === "object" && flagType._def) {
1121
+ return "object";
1122
+ }
1115
1123
  if (typeof flagType === "function") {
1116
1124
  if (flagType === String) return "string";
1117
1125
  if (flagType === Number) return "number";
@@ -3338,7 +3346,21 @@ const _ArgParserBase = class _ArgParserBase {
3338
3346
  const result = flag["type"](value);
3339
3347
  value = result && typeof result.then === "function" ? await result : result;
3340
3348
  } else if (typeof flag["type"] === "object") {
3341
- value = new flag["type"](value);
3349
+ if (flag["type"] && flag["type"]._def) {
3350
+ try {
3351
+ const parsedJson = typeof value === "string" ? JSON.parse(value) : value;
3352
+ value = flag["type"].parse(parsedJson);
3353
+ } catch (error) {
3354
+ if (error instanceof SyntaxError) {
3355
+ throw new Error(`Invalid JSON for flag '${flag["name"]}': ${error.message}`);
3356
+ } else {
3357
+ const errorMessage = error instanceof Error ? error.message : String(error);
3358
+ throw new Error(`Validation failed for flag '${flag["name"]}': ${errorMessage}`);
3359
+ }
3360
+ }
3361
+ } else {
3362
+ value = new flag["type"](value);
3363
+ }
3342
3364
  }
3343
3365
  if (flag["enum"] && flag["enum"].length > 0) {
3344
3366
  const allowedValues = flag["enum"].map((v) => typeof v === "string" ? `'${v}'` : v).join(", ");
@@ -3710,7 +3732,28 @@ ${cyan("Flags:")}
3710
3732
  const descriptionLines = Array.isArray(flag["description"]) ? flag["description"] : [flag["description"]];
3711
3733
  const metaLines = [];
3712
3734
  let typeName = "unknown";
3713
- if (typeof flag["type"] === "function") {
3735
+ let typeDetails = [];
3736
+ if (flag["type"] && typeof flag["type"] === "object" && flag["type"]._def) {
3737
+ typeName = "JSON object";
3738
+ try {
3739
+ const zodSchema = flag["type"];
3740
+ const def = zodSchema._def;
3741
+ if (def.shape) {
3742
+ const shape = typeof def.shape === "function" ? def.shape() : def.shape;
3743
+ const properties2 = Object.keys(shape);
3744
+ if (properties2.length > 0) {
3745
+ if (properties2.length <= 4) {
3746
+ typeDetails.push(`Properties: ${properties2.join(", ")}`);
3747
+ } else {
3748
+ typeDetails.push(`Properties: ${properties2.slice(0, 4).join(", ")}, ... (${properties2.length} total)`);
3749
+ }
3750
+ }
3751
+ }
3752
+ typeDetails.push("Expected: JSON string");
3753
+ } catch (error) {
3754
+ typeDetails.push("Expected: JSON string");
3755
+ }
3756
+ } else if (typeof flag["type"] === "function") {
3714
3757
  typeName = flag["type"].name || "custom function";
3715
3758
  if (typeName === "Boolean") typeName = "boolean";
3716
3759
  if (typeName === "String") typeName = "string";
@@ -3721,6 +3764,9 @@ ${cyan("Flags:")}
3721
3764
  typeName = flag["type"];
3722
3765
  }
3723
3766
  metaLines.push(`Type: ${typeName}`);
3767
+ if (typeDetails.length > 0) {
3768
+ metaLines.push(...typeDetails);
3769
+ }
3724
3770
  if (flag["flagOnly"]) {
3725
3771
  metaLines.push("Flag only (no value expected)");
3726
3772
  }
@@ -4473,7 +4519,9 @@ _buildRecursiveString_fn = function(parser, level, visited = /* @__PURE__ */ new
4473
4519
  `${flagIndent} Description: ${Array.isArray(flag["description"]) ? flag["description"].join(" | ") : flag["description"]}`
4474
4520
  );
4475
4521
  let typeName = "unknown";
4476
- if (typeof flag["type"] === "function") {
4522
+ if (flag["type"] && typeof flag["type"] === "object" && flag["type"]._def) {
4523
+ typeName = "Zod schema";
4524
+ } else if (typeof flag["type"] === "function") {
4477
4525
  typeName = flag["type"].name || "custom function";
4478
4526
  } else if (typeof flag["type"] === "string") {
4479
4527
  typeName = flag["type"];
@@ -4540,7 +4588,9 @@ _buildRecursiveJson_fn = function(parser, visited = /* @__PURE__ */ new Set()) {
4540
4588
  config.flags = flags.map((flag) => {
4541
4589
  var _a;
4542
4590
  let typeName = "unknown";
4543
- if (typeof flag["type"] === "function") {
4591
+ if (flag["type"] && typeof flag["type"] === "object" && flag["type"]._def) {
4592
+ typeName = "Zod schema";
4593
+ } else if (typeof flag["type"] === "function") {
4544
4594
  typeName = flag["type"].name || "custom function";
4545
4595
  } else if (typeof flag["type"] === "string") {
4546
4596
  typeName = flag["type"];
@@ -4963,6 +5013,25 @@ function createMcpErrorResponse(error) {
4963
5013
  };
4964
5014
  }
4965
5015
  function convertFlagToJsonSchemaProperty(flag) {
5016
+ if (flag.type && typeof flag.type === "object" && flag.type._def) {
5017
+ const zodSchema = flag.type;
5018
+ try {
5019
+ const property2 = z.toJSONSchema(zodSchema);
5020
+ if (flag.description) {
5021
+ property2.description = flag.description;
5022
+ }
5023
+ const isRequired2 = !!(flag.mandatory || flag.required);
5024
+ return { property: property2, isRequired: isRequired2 };
5025
+ } catch (error) {
5026
+ console.warn(`Failed to convert Zod schema to JSON Schema for flag '${flag.name}':`, error);
5027
+ const property2 = {
5028
+ type: "object",
5029
+ description: flag.description || `${flag.name} parameter (Zod schema)`
5030
+ };
5031
+ const isRequired2 = !!(flag.mandatory || flag.required);
5032
+ return { property: property2, isRequired: isRequired2 };
5033
+ }
5034
+ }
4966
5035
  const property = {
4967
5036
  type: getJsonSchemaTypeFromFlag(flag.type),
4968
5037
  description: flag.description || `${flag.name} parameter`
@@ -5069,8 +5138,11 @@ function extractSimplifiedResponse(mcpResponse) {
5069
5138
  };
5070
5139
  }
5071
5140
  function mapArgParserFlagToZodSchema(flag) {
5072
- let zodSchema = z.string();
5073
5141
  const flagTypeOpt = flag["type"];
5142
+ if (flagTypeOpt && typeof flagTypeOpt === "object" && flagTypeOpt._def) {
5143
+ return flagTypeOpt;
5144
+ }
5145
+ let zodSchema = z.string();
5074
5146
  let typeName;
5075
5147
  if (typeof flagTypeOpt === "function") {
5076
5148
  typeName = flagTypeOpt.name.toLowerCase().replace("constructor", "");