|
| 1 | +import { Options as AjvOptions } from "ajv" |
| 2 | +declare namespace build { |
| 3 | + interface BaseSchema { |
| 4 | + /** |
| 5 | + * Schema title |
| 6 | + */ |
| 7 | + title?: string; |
| 8 | + /** |
| 9 | + * Schema description |
| 10 | + */ |
| 11 | + description?: string; |
| 12 | + /** |
| 13 | + * A comment to be added to the schema |
| 14 | + */ |
| 15 | + $comment?: string; |
| 16 | + /** |
| 17 | + * Default value to be assigned when no value is given in the document |
| 18 | + */ |
| 19 | + default?: any; |
| 20 | + /** |
| 21 | + * A list of example values that match this schema |
| 22 | + */ |
| 23 | + examples?: any[]; |
| 24 | + /** |
| 25 | + * Additional schema definition to reference from within the schema |
| 26 | + */ |
| 27 | + definitions?: Record<string, Schema> |
| 28 | + /** |
| 29 | + * A set of schemas of which at least one must match |
| 30 | + */ |
| 31 | + anyOf?: Partial<Schema>[]; |
| 32 | + /** |
| 33 | + * A set of schemas which must all match |
| 34 | + */ |
| 35 | + allOf?: Partial<Schema>[]; |
| 36 | + /** |
| 37 | + * A conditional schema to check, controls schemas defined in `then` and `else` |
| 38 | + */ |
| 39 | + if?: Partial<Schema>; |
| 40 | + /** |
| 41 | + * A schema to apply if the conditional schema from `if` passes |
| 42 | + */ |
| 43 | + then?: Partial<Schema>; |
| 44 | + /** |
| 45 | + * A schema to apply if the conditional schema from `if` fails |
| 46 | + */ |
| 47 | + else?: Partial<Schema>; |
| 48 | + /** |
| 49 | + * Open API 3.0 spec states that any value that can be null must be declared `nullable` |
| 50 | + * @default false |
| 51 | + */ |
| 52 | + nullable?: boolean; |
| 53 | + } |
| 54 | + |
| 55 | + export interface RefSchema { |
| 56 | + /** |
| 57 | + * A json-pointer to a schema to use as a reference |
| 58 | + */ |
| 59 | + $ref: string; |
| 60 | + } |
| 61 | + |
| 62 | + export interface StringSchema extends BaseSchema { |
| 63 | + type: "string"; |
| 64 | + } |
| 65 | + |
| 66 | + export interface IntegerSchema extends BaseSchema { |
| 67 | + type: "integer"; |
| 68 | + } |
| 69 | + |
| 70 | + export interface NumberSchema extends BaseSchema { |
| 71 | + type: "number"; |
| 72 | + } |
| 73 | + |
| 74 | + export interface NullSchema extends BaseSchema { |
| 75 | + type: "null"; |
| 76 | + } |
| 77 | + |
| 78 | + export interface BooleanSchema extends BaseSchema { |
| 79 | + type: "boolean"; |
| 80 | + } |
| 81 | + |
| 82 | + export interface ArraySchema extends BaseSchema { |
| 83 | + type: "array"; |
| 84 | + /** |
| 85 | + * The schema for the items in the array |
| 86 | + */ |
| 87 | + items: Schema | {} |
| 88 | + } |
| 89 | + |
| 90 | + export interface TupleSchema extends BaseSchema { |
| 91 | + type: "array"; |
| 92 | + /** |
| 93 | + * The schemas for the items in the tuple |
| 94 | + */ |
| 95 | + items: Schema[]; |
| 96 | + } |
| 97 | + |
| 98 | + type ObjectProperties = Record<string, Partial<Schema>> & { |
| 99 | + anyOf?: ObjectProperties[]; |
| 100 | + allOf?: ObjectProperties[]; |
| 101 | + if?: ObjectProperties; |
| 102 | + then?: ObjectProperties; |
| 103 | + else?: ObjectProperties; |
| 104 | + } |
| 105 | + |
| 106 | + export interface ObjectSchema extends BaseSchema { |
| 107 | + type: "object"; |
| 108 | + /** |
| 109 | + * Describe the properties of the object |
| 110 | + */ |
| 111 | + properties?: ObjectProperties; |
| 112 | + /** |
| 113 | + * The required properties of the object |
| 114 | + */ |
| 115 | + required?: string[]; |
| 116 | + /** |
| 117 | + * Describe properties that have keys following a given pattern |
| 118 | + */ |
| 119 | + patternProperties?: ObjectProperties; |
| 120 | + /** |
| 121 | + * Specifies whether additional properties on the object are allowed, and optionally what schema they should |
| 122 | + * adhere to |
| 123 | + * @default false |
| 124 | + */ |
| 125 | + additionalProperties?: Schema | boolean; |
| 126 | + } |
| 127 | + |
| 128 | + export type Schema = |
| 129 | + | RefSchema |
| 130 | + | StringSchema |
| 131 | + | IntegerSchema |
| 132 | + | NumberSchema |
| 133 | + | NullSchema |
| 134 | + | BooleanSchema |
| 135 | + | ArraySchema |
| 136 | + | TupleSchema |
| 137 | + | ObjectSchema; |
| 138 | + |
| 139 | + export interface Options { |
| 140 | + /** |
| 141 | + * Optionally add an external definition to reference from your schema |
| 142 | + */ |
| 143 | + schema?: Record<string, Schema> |
| 144 | + /** |
| 145 | + * Uglify the generated serialization function to get a performance increase on Node.js versions lower than 8.3.0 |
| 146 | + */ |
| 147 | + uglify?: boolean |
| 148 | + /** |
| 149 | + * Configure Ajv, which is used to evaluate conditional schemas and combined (anyOf) schemas |
| 150 | + */ |
| 151 | + ajv?: AjvOptions |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +/** |
| 156 | + * Build a stringify function using a schema of the documents that should be stringified |
| 157 | + * @param schema The schema used to stringify values |
| 158 | + * @param options The options to use (optional) |
| 159 | + */ |
| 160 | +declare function build(schema: build.StringSchema, options?: build.Options): (doc: string) => string; |
| 161 | +declare function build(schema: build.IntegerSchema | build.NumberSchema, options?: build.Options): (doc: number) => string; |
| 162 | +declare function build(schema: build.NullSchema, options?: build.Options): (doc: null) => "null"; |
| 163 | +declare function build(schema: build.BooleanSchema, options?: build.Options): (doc: boolean) => string; |
| 164 | +declare function build(schema: build.ArraySchema | build.TupleSchema, options?: build.Options): (doc: any[]) => string; |
| 165 | +declare function build(schema: build.ObjectSchema, options?: build.Options): (doc: object) => string; |
| 166 | +declare function build(schema: build.Schema, options?: build.Options): (doc: object | any[] | string | number | boolean | null) => string; |
| 167 | + |
| 168 | +export = build; |
0 commit comments