index.d.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /** The Standard Typed interface. This is a base type extended by other specs. */
  2. interface StandardTypedV1<Input = unknown, Output = Input> {
  3. /** The Standard properties. */
  4. readonly "~standard": StandardTypedV1.Props<Input, Output>;
  5. }
  6. declare namespace StandardTypedV1 {
  7. /** The Standard Typed properties interface. */
  8. interface Props<Input = unknown, Output = Input> {
  9. /** The version number of the standard. */
  10. readonly version: 1;
  11. /** The vendor name of the schema library. */
  12. readonly vendor: string;
  13. /** Inferred types associated with the schema. */
  14. readonly types?: Types<Input, Output> | undefined;
  15. }
  16. /** The Standard Typed types interface. */
  17. interface Types<Input = unknown, Output = Input> {
  18. /** The input type of the schema. */
  19. readonly input: Input;
  20. /** The output type of the schema. */
  21. readonly output: Output;
  22. }
  23. /** Infers the input type of a Standard Typed. */
  24. type InferInput<Schema extends StandardTypedV1> = NonNullable<Schema["~standard"]["types"]>["input"];
  25. /** Infers the output type of a Standard Typed. */
  26. type InferOutput<Schema extends StandardTypedV1> = NonNullable<Schema["~standard"]["types"]>["output"];
  27. }
  28. /** The Standard Schema interface. */
  29. interface StandardSchemaV1<Input = unknown, Output = Input> {
  30. /** The Standard Schema properties. */
  31. readonly "~standard": StandardSchemaV1.Props<Input, Output>;
  32. }
  33. declare namespace StandardSchemaV1 {
  34. /** The Standard Schema properties interface. */
  35. interface Props<Input = unknown, Output = Input> extends StandardTypedV1.Props<Input, Output> {
  36. /** Validates unknown input values. */
  37. readonly validate: (value: unknown, options?: StandardSchemaV1.Options | undefined) => Result<Output> | Promise<Result<Output>>;
  38. }
  39. /** The result interface of the validate function. */
  40. type Result<Output> = SuccessResult<Output> | FailureResult;
  41. /** The result interface if validation succeeds. */
  42. interface SuccessResult<Output> {
  43. /** The typed output value. */
  44. readonly value: Output;
  45. /** A falsy value for `issues` indicates success. */
  46. readonly issues?: undefined;
  47. }
  48. interface Options {
  49. /** Explicit support for additional vendor-specific parameters, if needed. */
  50. readonly libraryOptions?: Record<string, unknown> | undefined;
  51. }
  52. /** The result interface if validation fails. */
  53. interface FailureResult {
  54. /** The issues of failed validation. */
  55. readonly issues: ReadonlyArray<Issue>;
  56. }
  57. /** The issue interface of the failure output. */
  58. interface Issue {
  59. /** The error message of the issue. */
  60. readonly message: string;
  61. /** The path of the issue, if any. */
  62. readonly path?: ReadonlyArray<PropertyKey | PathSegment> | undefined;
  63. }
  64. /** The path segment interface of the issue. */
  65. interface PathSegment {
  66. /** The key representing a path segment. */
  67. readonly key: PropertyKey;
  68. }
  69. /** The Standard types interface. */
  70. interface Types<Input = unknown, Output = Input> extends StandardTypedV1.Types<Input, Output> {
  71. }
  72. /** Infers the input type of a Standard. */
  73. type InferInput<Schema extends StandardTypedV1> = StandardTypedV1.InferInput<Schema>;
  74. /** Infers the output type of a Standard. */
  75. type InferOutput<Schema extends StandardTypedV1> = StandardTypedV1.InferOutput<Schema>;
  76. }
  77. /** The Standard JSON Schema interface. */
  78. interface StandardJSONSchemaV1<Input = unknown, Output = Input> {
  79. /** The Standard JSON Schema properties. */
  80. readonly "~standard": StandardJSONSchemaV1.Props<Input, Output>;
  81. }
  82. declare namespace StandardJSONSchemaV1 {
  83. /** The Standard JSON Schema properties interface. */
  84. interface Props<Input = unknown, Output = Input> extends StandardTypedV1.Props<Input, Output> {
  85. /** Methods for generating the input/output JSON Schema. */
  86. readonly jsonSchema: StandardJSONSchemaV1.Converter;
  87. }
  88. /** The Standard JSON Schema converter interface. */
  89. interface Converter {
  90. /** Converts the input type to JSON Schema. May throw if conversion is not supported. */
  91. readonly input: (options: StandardJSONSchemaV1.Options) => Record<string, unknown>;
  92. /** Converts the output type to JSON Schema. May throw if conversion is not supported. */
  93. readonly output: (options: StandardJSONSchemaV1.Options) => Record<string, unknown>;
  94. }
  95. /**
  96. * The target version of the generated JSON Schema.
  97. *
  98. * It is *strongly recommended* that implementers support `"draft-2020-12"` and `"draft-07"`, as they are both in wide use. All other targets can be implemented on a best-effort basis. Libraries should throw if they don't support a specified target.
  99. *
  100. * The `"openapi-3.0"` target is intended as a standardized specifier for OpenAPI 3.0 which is a superset of JSON Schema `"draft-04"`.
  101. */
  102. type Target = "draft-2020-12" | "draft-07" | "openapi-3.0" | ({} & string);
  103. /** The options for the input/output methods. */
  104. interface Options {
  105. /** Specifies the target version of the generated JSON Schema. Support for all versions is on a best-effort basis. If a given version is not supported, the library should throw. */
  106. readonly target: Target;
  107. /** Explicit support for additional vendor-specific parameters, if needed. */
  108. readonly libraryOptions?: Record<string, unknown> | undefined;
  109. }
  110. /** The Standard types interface. */
  111. interface Types<Input = unknown, Output = Input> extends StandardTypedV1.Types<Input, Output> {
  112. }
  113. /** Infers the input type of a Standard. */
  114. type InferInput<Schema extends StandardTypedV1> = StandardTypedV1.InferInput<Schema>;
  115. /** Infers the output type of a Standard. */
  116. type InferOutput<Schema extends StandardTypedV1> = StandardTypedV1.InferOutput<Schema>;
  117. }
  118. export { StandardJSONSchemaV1, StandardSchemaV1, StandardTypedV1 };