Skip to content

Commit 5d49f80

Browse files
tmcwmcollina
authored andcommitted
Fix coercion types
1 parent 74c35c0 commit 5d49f80

File tree

2 files changed

+84
-59
lines changed

2 files changed

+84
-59
lines changed

Diff for: types/index.d.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,9 @@ interface StandaloneOption extends build.Options {
208208
mode: 'standalone'
209209
}
210210

211+
type StringCoercible = string | Date | RegExp;
212+
type IntegerCoercible = number | BigInt;
213+
211214
/**
212215
* Build a stringify function using a schema of the documents that should be stringified
213216
* @param schema The schema used to stringify values
@@ -217,8 +220,8 @@ declare function build(schema: build.AnySchema, options: DebugOption): { code: s
217220
declare function build(schema: build.AnySchema, options: DeprecateDebugOption): { code: string, ajv: Ajv };
218221
declare function build(schema: build.AnySchema, options: StandaloneOption): string;
219222
declare function build(schema: build.AnySchema, options?: build.Options): <TDoc = any>(doc: TDoc) => any;
220-
declare function build(schema: build.StringSchema, options?: build.Options): <TDoc extends string = string>(doc: TDoc) => string;
221-
declare function build(schema: build.IntegerSchema | build.NumberSchema, options?: build.Options): <TDoc extends number = number>(doc: TDoc) => string;
223+
declare function build(schema: build.StringSchema, options?: build.Options): <TDoc extends StringCoercible = StringCoercible>(doc: TDoc) => string;
224+
declare function build(schema: build.IntegerSchema | build.NumberSchema, options?: build.Options): <TDoc extends IntegerCoercible = IntegerCoercible>(doc: TDoc) => string;
222225
declare function build(schema: build.NullSchema, options?: build.Options): <TDoc extends null = null>(doc: TDoc) => "null";
223226
declare function build(schema: build.BooleanSchema, options?: build.Options): <TDoc extends boolean = boolean>(doc: TDoc) => string;
224227
declare function build(schema: build.ArraySchema | build.TupleSchema, options?: build.Options): <TDoc extends any[]= any[]>(doc: TDoc) => string;

Diff for: types/index.test-d.ts

+79-57
Original file line numberDiff line numberDiff line change
@@ -3,61 +3,62 @@ import build, { restore, Schema, validLargeArrayMechanisms } from '..'
33
import { expectError, expectType } from 'tsd'
44

55
// Number schemas
6-
const schema1: Schema = {
6+
build({
77
type: 'number'
8-
}
9-
const schema2: Schema = {
8+
})(25)
9+
build({
1010
type: 'integer'
11-
}
12-
13-
build(schema1)(25)
14-
build(schema2)(-5)
11+
})(-5)
12+
build({
13+
type: 'integer'
14+
})(5n)
1515

16-
build(schema2, { rounding: 'ceil' })
17-
build(schema2, { rounding: 'floor' })
18-
build(schema2, { rounding: 'round' })
19-
build(schema2, { rounding: 'trunc' })
20-
expectError(build(schema2, { rounding: 'invalid' }))
16+
build({
17+
type: 'number'
18+
}, { rounding: 'ceil' })
19+
build({
20+
type: 'number'
21+
}, { rounding: 'floor' })
22+
build({
23+
type: 'number'
24+
}, { rounding: 'round' })
25+
build({
26+
type: 'number'
27+
}, { rounding: 'trunc' })
28+
expectError(build({
29+
type: 'number'
30+
}, { rounding: 'invalid' }))
2131

2232
// String schema
23-
const schema3: Schema = {
24-
type: 'string'
25-
}
26-
27-
build(schema3)('foobar')
33+
build({
34+
type: 'string'
35+
})('foobar')
2836

2937
// Boolean schema
30-
const schema4: Schema = {
38+
build({
3139
type: 'boolean'
32-
}
33-
34-
build(schema4)(true)
40+
})(true)
3541

3642
// Null schema
37-
const schema5: Schema = {
43+
build({
3844
type: 'null'
39-
}
40-
41-
build(schema5)(null)
45+
})(null)
4246

4347
// Array schemas
44-
const schema6: Schema = {
48+
build({
4549
type: 'array',
4650
items: { type: 'number' }
47-
}
48-
const schema7: Schema = {
51+
})([25])
52+
build({
4953
type: 'array',
5054
items: [{ type: 'string'}, {type: 'integer'}]
51-
}
52-
53-
build(schema6)([25])
54-
build(schema7)(['hello', 42])
55+
})(['hello', 42])
5556

5657
// Object schemas
57-
const schema8: Schema = {
58+
build({
5859
type: 'object'
59-
}
60-
const schema9: Schema = {
60+
})({})
61+
build({
6162
type: 'object',
6263
properties: {
6364
foo: { type: 'string' },
@@ -70,14 +71,24 @@ const schema9: Schema = {
7071
additionalProperties: {
7172
type: 'boolean'
7273
}
73-
}
74-
75-
build(schema8)({})
76-
build(schema9)({ foo: 'bar' })
77-
build(schema9, { rounding: 'floor' })({ foo: 'bar' })
74+
})({ foo: 'bar' })
75+
build({
76+
type: 'object',
77+
properties: {
78+
foo: { type: 'string' },
79+
bar: { type: 'integer' }
80+
},
81+
required: ['foo'],
82+
patternProperties: {
83+
'baz*': { type: 'null' }
84+
},
85+
additionalProperties: {
86+
type: 'boolean'
87+
}
88+
}, { rounding: 'floor' })({ foo: 'bar' })
7889

7990
// Reference schemas
80-
const schema10: Schema = {
91+
build({
8192
title: 'Example Schema',
8293
definitions: {
8394
num: {
@@ -109,12 +120,10 @@ const schema10: Schema = {
109120
additionalProperties: {
110121
$ref: '#/definitions/def'
111122
}
112-
}
113-
114-
build(schema10)({ nickname: '', num: { int: 5 }, other: null })
123+
})({ nickname: '', num: { int: 5 }, other: null })
115124

116125
// Conditional/Combined schemas
117-
const schema11: Schema = {
126+
build({
118127
title: 'Conditional/Combined Schema',
119128
type: 'object',
120129
properties: {
@@ -140,24 +149,37 @@ const schema11: Schema = {
140149
somethingElse: { type: 'null' }
141150
}
142151
}
143-
}
144-
145-
build(schema11)({ something: 'a string', somethingElse: 42 })
152+
})({ something: 'a string', somethingElse: 42 })
146153

147154
// String schema with format
148-
const schema12: Schema = {
155+
156+
build({
149157
type: 'string',
150158
format: 'date-time'
151-
}
152-
153-
build(schema12)(new Date())
159+
})(new Date())
154160

161+
/*
162+
This overload doesn't work yet -
163+
TypeScript chooses the generic for the schema
164+
before it chooses the overload for the options
165+
parameter.
155166
let str: string, ajv: Ajv
156-
str = build(schema1, { debugMode: true }).code
157-
ajv = build(schema1, { debugMode: true }).ajv
158-
str = build(schema1, { mode: 'debug' }).code
159-
ajv = build(schema1, { mode: 'debug' }).ajv
160-
str = build(schema1, { mode: 'standalone' })
167+
str = build({
168+
type: 'number'
169+
}, { debugMode: true }).code
170+
ajv = build({
171+
type: 'number'
172+
}, { debugMode: true }).ajv
173+
str = build({
174+
type: 'number'
175+
}, { mode: 'debug' }).code
176+
ajv = build({
177+
type: 'number'
178+
}, { mode: 'debug' }).ajv
179+
str = build({
180+
type: 'number'
181+
}, { mode: 'standalone' })
182+
*/
161183

162184
const debugCompiled = build({
163185
title: 'default string',

0 commit comments

Comments
 (0)