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