{"version":3,"file":"standard-schema.mjs","names":[],"sources":["../../../../../../../@warlock.js/fs/src/facade/standard-schema.ts"],"sourcesContent":["/**\n * Minimal, vendor-neutral [Standard Schema](https://standardschema.dev) v1\n * interface, declared locally so `@warlock.js/fs` takes **zero dependencies**\n * (not even a type-only one) to support schema-validated JSON reads.\n *\n * Any Standard-Schema-compliant validator satisfies this — `@warlock.js/seal`\n * (every seal validator is `& StandardSchemaV1`), zod, valibot, etc. — so\n * `fs.files.getJson(path, { schema })` is validator-agnostic and validates by\n * calling the schema's own `~standard.validate`, with no runtime import.\n */\nexport interface StandardSchemaV1<Output = unknown> {\n  readonly \"~standard\": {\n    readonly version: 1;\n    readonly vendor: string;\n    readonly validate: (\n      value: unknown,\n    ) => StandardSchemaResult<Output> | Promise<StandardSchemaResult<Output>>;\n  };\n}\n\n/** A Standard Schema validation issue. */\nexport type StandardSchemaIssue = {\n  readonly message: string;\n  readonly path?: ReadonlyArray<PropertyKey | { readonly key: PropertyKey }>;\n};\n\n/** Success carries the (possibly transformed) value; failure carries issues. */\nexport type StandardSchemaResult<Output> =\n  | { readonly value: Output; readonly issues?: undefined }\n  | { readonly issues: ReadonlyArray<StandardSchemaIssue> };\n\n/**\n * Thrown when a `getJson({ schema })` read fails Standard Schema validation.\n * Carries the raw issues so callers can inspect what failed.\n */\nexport class JsonSchemaValidationError extends Error {\n  public constructor(\n    public readonly path: string,\n    public readonly issues: ReadonlyArray<StandardSchemaIssue>,\n  ) {\n    const summary = issues.map((issue) => issue.message).join(\"; \");\n    super(`JSON at \"${path}\" failed schema validation: ${summary}`);\n    this.name = \"JsonSchemaValidationError\";\n  }\n}\n\n/**\n * Validate an already-parsed value against a Standard Schema. Awaits the\n * (possibly async) `~standard.validate`, returns the validated/transformed\n * value on success, and throws {@link JsonSchemaValidationError} on failure.\n *\n * @param path - Source path, for the error message.\n * @param schema - Any Standard Schema validator.\n * @param value - The parsed JSON value to validate.\n * @returns The validated (possibly transformed) value.\n */\nexport async function validateAgainstSchema<T>(\n  path: string,\n  schema: StandardSchemaV1<T>,\n  value: unknown,\n): Promise<T> {\n  const result = await schema[\"~standard\"].validate(value);\n\n  if (\"issues\" in result && result.issues) {\n    throw new JsonSchemaValidationError(path, result.issues);\n  }\n\n  return (result as { value: T }).value;\n}\n"],"mappings":";;;;;AAmCA,IAAa,4BAAb,cAA+C,MAAM;CAEjC;CACA;CAFlB,AAAO,YACL,AAAgB,MAChB,AAAgB,QAChB;EACA,MAAM,UAAU,OAAO,KAAK,UAAU,MAAM,OAAO,CAAC,CAAC,KAAK,IAAI;EAC9D,MAAM,YAAY,KAAK,8BAA8B,SAAS;EAJ9C;EACA;EAIhB,KAAK,OAAO;CACd;AACF;;;;;;;;;;;AAYA,eAAsB,sBACpB,MACA,QACA,OACY;CACZ,MAAM,SAAS,MAAM,OAAO,YAAY,CAAC,SAAS,KAAK;CAEvD,IAAI,YAAY,UAAU,OAAO,QAC/B,MAAM,IAAI,0BAA0B,MAAM,OAAO,MAAM;CAGzD,OAAQ,OAAwB;AAClC"}