standard-schema.d.cts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /**
  2. * The Standard Schema interface.
  3. */
  4. export type StandardSchemaV1<Input = unknown, Output = Input> = {
  5. /**
  6. * The Standard Schema properties.
  7. */
  8. readonly "~standard": StandardSchemaV1.Props<Input, Output>;
  9. };
  10. export declare namespace StandardSchemaV1 {
  11. /**
  12. * The Standard Schema properties interface.
  13. */
  14. export interface Props<Input = unknown, Output = Input> {
  15. /**
  16. * The version number of the standard.
  17. */
  18. readonly version: 1;
  19. /**
  20. * The vendor name of the schema library.
  21. */
  22. readonly vendor: string;
  23. /**
  24. * Validates unknown input values.
  25. */
  26. readonly validate: (value: unknown) => Result<Output> | Promise<Result<Output>>;
  27. /**
  28. * Inferred types associated with the schema.
  29. */
  30. readonly types?: Types<Input, Output> | undefined;
  31. }
  32. /**
  33. * The result interface of the validate function.
  34. */
  35. export type Result<Output> = SuccessResult<Output> | FailureResult;
  36. /**
  37. * The result interface if validation succeeds.
  38. */
  39. export interface SuccessResult<Output> {
  40. /**
  41. * The typed output value.
  42. */
  43. readonly value: Output;
  44. /**
  45. * The non-existent issues.
  46. */
  47. readonly issues?: undefined;
  48. }
  49. /**
  50. * The result interface if validation fails.
  51. */
  52. export interface FailureResult {
  53. /**
  54. * The issues of failed validation.
  55. */
  56. readonly issues: ReadonlyArray<Issue>;
  57. }
  58. /**
  59. * The issue interface of the failure output.
  60. */
  61. export interface Issue {
  62. /**
  63. * The error message of the issue.
  64. */
  65. readonly message: string;
  66. /**
  67. * The path of the issue, if any.
  68. */
  69. readonly path?: ReadonlyArray<PropertyKey | PathSegment> | undefined;
  70. }
  71. /**
  72. * The path segment interface of the issue.
  73. */
  74. export interface PathSegment {
  75. /**
  76. * The key representing a path segment.
  77. */
  78. readonly key: PropertyKey;
  79. }
  80. /**
  81. * The Standard Schema types interface.
  82. */
  83. export interface Types<Input = unknown, Output = Input> {
  84. /**
  85. * The input type of the schema.
  86. */
  87. readonly input: Input;
  88. /**
  89. * The output type of the schema.
  90. */
  91. readonly output: Output;
  92. }
  93. /**
  94. * Infers the input type of a Standard Schema.
  95. */
  96. export type InferInput<Schema extends StandardSchemaV1> = NonNullable<Schema["~standard"]["types"]>["input"];
  97. /**
  98. * Infers the output type of a Standard Schema.
  99. */
  100. export type InferOutput<Schema extends StandardSchemaV1> = NonNullable<Schema["~standard"]["types"]>["output"];
  101. export {};
  102. }