fast-json-stringify 6.0.0 → 6.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/dependabot.yml +1 -1
- package/.github/workflows/benchmark.yml +3 -3
- package/.github/workflows/ci.yml +1 -1
- package/README.md +15 -15
- package/benchmark/bench-cmp-lib.js +0 -1
- package/eslint.config.js +9 -0
- package/index.js +4 -8
- package/lib/serializer.js +1 -2
- package/lib/validator.js +1 -1
- package/package.json +40 -16
- package/test/additionalProperties.test.js +20 -20
- package/test/allof.test.js +36 -40
- package/test/any.test.js +28 -28
- package/test/anyof.test.js +44 -46
- package/test/array.test.js +19 -19
- package/test/asNumber.test.js +2 -2
- package/test/basic.test.js +20 -32
- package/test/bigint.test.js +6 -6
- package/test/clean-cache.test.js +9 -7
- package/test/const.test.js +35 -35
- package/test/date.test.js +77 -77
- package/test/debug-mode.test.js +27 -27
- package/test/defaults.test.js +2 -2
- package/test/enum.test.js +3 -3
- package/test/fix-604.test.js +18 -17
- package/test/if-then-else.test.js +22 -24
- package/test/inferType.test.js +4 -4
- package/test/infinity.test.js +4 -4
- package/test/integer.test.js +21 -21
- package/test/invalidSchema.test.js +4 -8
- package/test/issue-479.test.js +2 -2
- package/test/json-schema-test-suite/draft4.test.js +3 -3
- package/test/json-schema-test-suite/draft6.test.js +3 -3
- package/test/json-schema-test-suite/draft7.test.js +3 -3
- package/test/json-schema-test-suite/util.js +6 -9
- package/test/missing-values.test.js +8 -8
- package/test/multi-type-serializer.test.js +2 -2
- package/test/nestedObjects.test.js +3 -3
- package/test/nullable.test.js +35 -35
- package/test/oneof.test.js +27 -29
- package/test/patternProperties.test.js +11 -11
- package/test/recursion.test.js +6 -6
- package/test/ref.test.js +102 -186
- package/test/regex.test.js +4 -5
- package/test/required.test.js +42 -77
- package/test/requiresAjv.test.js +34 -32
- package/test/sanitize.test.js +40 -41
- package/test/sanitize2.test.js +11 -11
- package/test/sanitize3.test.js +13 -11
- package/test/sanitize4.test.js +10 -8
- package/test/sanitize5.test.js +11 -11
- package/test/sanitize6.test.js +14 -14
- package/test/sanitize7.test.js +5 -5
- package/test/side-effect.test.js +12 -12
- package/test/standalone-mode.test.js +29 -26
- package/test/string.test.js +12 -12
- package/test/surrogate.test.js +9 -9
- package/test/toJSON.test.js +9 -9
- package/test/typebox.test.js +2 -2
- package/test/typesArray.test.js +34 -34
- package/test/unknownFormats.test.js +5 -5
- package/test/webpack.test.js +2 -2
- package/types/index.d.ts +29 -29
- package/types/index.test-d.ts +75 -74
- package/.taprc +0 -7
package/types/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import Ajv, { Options as AjvOptions } from
|
|
1
|
+
import Ajv, { Options as AjvOptions } from 'ajv'
|
|
2
2
|
|
|
3
3
|
type Build = typeof build
|
|
4
4
|
|
|
@@ -65,33 +65,33 @@ declare namespace build {
|
|
|
65
65
|
*/
|
|
66
66
|
$ref: string;
|
|
67
67
|
}
|
|
68
|
-
|
|
68
|
+
|
|
69
69
|
export interface AnySchema extends BaseSchema {
|
|
70
70
|
}
|
|
71
71
|
|
|
72
72
|
export interface StringSchema extends BaseSchema {
|
|
73
|
-
type:
|
|
73
|
+
type: 'string';
|
|
74
74
|
format?: string;
|
|
75
75
|
}
|
|
76
76
|
|
|
77
77
|
export interface IntegerSchema extends BaseSchema {
|
|
78
|
-
type:
|
|
78
|
+
type: 'integer';
|
|
79
79
|
}
|
|
80
80
|
|
|
81
81
|
export interface NumberSchema extends BaseSchema {
|
|
82
|
-
type:
|
|
82
|
+
type: 'number';
|
|
83
83
|
}
|
|
84
84
|
|
|
85
85
|
export interface NullSchema extends BaseSchema {
|
|
86
|
-
type:
|
|
86
|
+
type: 'null';
|
|
87
87
|
}
|
|
88
88
|
|
|
89
89
|
export interface BooleanSchema extends BaseSchema {
|
|
90
|
-
type:
|
|
90
|
+
type: 'boolean';
|
|
91
91
|
}
|
|
92
92
|
|
|
93
93
|
export interface ArraySchema extends BaseSchema {
|
|
94
|
-
type:
|
|
94
|
+
type: 'array';
|
|
95
95
|
/**
|
|
96
96
|
* The schema for the items in the array
|
|
97
97
|
*/
|
|
@@ -99,7 +99,7 @@ declare namespace build {
|
|
|
99
99
|
}
|
|
100
100
|
|
|
101
101
|
export interface TupleSchema extends BaseSchema {
|
|
102
|
-
type:
|
|
102
|
+
type: 'array';
|
|
103
103
|
/**
|
|
104
104
|
* The schemas for the items in the tuple
|
|
105
105
|
*/
|
|
@@ -115,7 +115,7 @@ declare namespace build {
|
|
|
115
115
|
}
|
|
116
116
|
|
|
117
117
|
export interface ObjectSchema extends BaseSchema {
|
|
118
|
-
type:
|
|
118
|
+
type: 'object';
|
|
119
119
|
/**
|
|
120
120
|
* Describe the properties of the object
|
|
121
121
|
*/
|
|
@@ -145,7 +145,7 @@ declare namespace build {
|
|
|
145
145
|
| BooleanSchema
|
|
146
146
|
| ArraySchema
|
|
147
147
|
| TupleSchema
|
|
148
|
-
| ObjectSchema
|
|
148
|
+
| ObjectSchema
|
|
149
149
|
|
|
150
150
|
export interface Options {
|
|
151
151
|
/**
|
|
@@ -158,12 +158,12 @@ declare namespace build {
|
|
|
158
158
|
ajv?: AjvOptions
|
|
159
159
|
/**
|
|
160
160
|
* Optionally configure how the integer will be rounded
|
|
161
|
-
*
|
|
161
|
+
*
|
|
162
162
|
* @default 'trunc'
|
|
163
163
|
*/
|
|
164
164
|
rounding?: 'ceil' | 'floor' | 'round' | 'trunc'
|
|
165
165
|
/**
|
|
166
|
-
* @deprecated
|
|
166
|
+
* @deprecated
|
|
167
167
|
* Enable debug mode. Please use `mode: "debug"` instead
|
|
168
168
|
*/
|
|
169
169
|
debugMode?: boolean
|
|
@@ -190,7 +190,7 @@ declare namespace build {
|
|
|
190
190
|
}
|
|
191
191
|
|
|
192
192
|
export const validLargeArrayMechanisms: string[]
|
|
193
|
-
export function restore (value: <TDoc extends object = object>(doc: TDoc) => string): ReturnType<Build>
|
|
193
|
+
export function restore (value: <TDoc extends object = object>(doc: TDoc) => string): ReturnType<Build>
|
|
194
194
|
|
|
195
195
|
export const build: Build
|
|
196
196
|
export { build as default }
|
|
@@ -208,24 +208,24 @@ interface StandaloneOption extends build.Options {
|
|
|
208
208
|
mode: 'standalone'
|
|
209
209
|
}
|
|
210
210
|
|
|
211
|
-
type StringCoercible = string | Date | RegExp
|
|
212
|
-
type IntegerCoercible = number | BigInt
|
|
211
|
+
type StringCoercible = string | Date | RegExp
|
|
212
|
+
type IntegerCoercible = number | BigInt
|
|
213
213
|
|
|
214
214
|
/**
|
|
215
215
|
* Build a stringify function using a schema of the documents that should be stringified
|
|
216
216
|
* @param schema The schema used to stringify values
|
|
217
217
|
* @param options The options to use (optional)
|
|
218
218
|
*/
|
|
219
|
-
declare function build(schema: build.AnySchema, options: DebugOption): { code: string, ajv: Ajv }
|
|
220
|
-
declare function build(schema: build.AnySchema, options: DeprecateDebugOption): { code: string, ajv: Ajv }
|
|
221
|
-
declare function build(schema: build.AnySchema, options: StandaloneOption): string
|
|
222
|
-
declare function build(schema: build.AnySchema, options?: build.Options): <TDoc = any>(doc: TDoc) => any
|
|
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
|
|
225
|
-
declare function build(schema: build.NullSchema, options?: build.Options): <TDoc extends null = null>(doc: TDoc) =>
|
|
226
|
-
declare function build(schema: build.BooleanSchema, options?: build.Options): <TDoc extends boolean = boolean>(doc: TDoc) => string
|
|
227
|
-
declare function build(schema: build.ArraySchema | build.TupleSchema, options?: build.Options): <TDoc extends any[]= any[]>(doc: TDoc) => string
|
|
228
|
-
declare function build(schema: build.ObjectSchema, options?: build.Options): <TDoc extends object = object>(doc: TDoc) => string
|
|
229
|
-
declare function build(schema: build.Schema, options?: build.Options): <TDoc = object | any[] | string | number | boolean | null> (doc: TDoc) => string
|
|
230
|
-
|
|
231
|
-
export = build
|
|
219
|
+
declare function build (schema: build.AnySchema, options: DebugOption): { code: string, ajv: Ajv }
|
|
220
|
+
declare function build (schema: build.AnySchema, options: DeprecateDebugOption): { code: string, ajv: Ajv }
|
|
221
|
+
declare function build (schema: build.AnySchema, options: StandaloneOption): string
|
|
222
|
+
declare function build (schema: build.AnySchema, options?: build.Options): <TDoc = any>(doc: TDoc) => any
|
|
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
|
|
225
|
+
declare function build (schema: build.NullSchema, options?: build.Options): <TDoc extends null = null>(doc: TDoc) => 'null'
|
|
226
|
+
declare function build (schema: build.BooleanSchema, options?: build.Options): <TDoc extends boolean = boolean>(doc: TDoc) => string
|
|
227
|
+
declare function build (schema: build.ArraySchema | build.TupleSchema, options?: build.Options): <TDoc extends any[]= any[]>(doc: TDoc) => string
|
|
228
|
+
declare function build (schema: build.ObjectSchema, options?: build.Options): <TDoc extends object = object>(doc: TDoc) => string
|
|
229
|
+
declare function build (schema: build.Schema, options?: build.Options): <TDoc = object | any[] | string | number | boolean | null> (doc: TDoc) => string
|
|
230
|
+
|
|
231
|
+
export = build
|
package/types/index.test-d.ts
CHANGED
|
@@ -1,32 +1,33 @@
|
|
|
1
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars -- Test using this disabled, see https://github.com/fastify/fast-json-stringify/pull/683
|
|
1
2
|
import Ajv from 'ajv'
|
|
2
3
|
import build, { restore, Schema, validLargeArrayMechanisms } from '..'
|
|
3
4
|
import { expectError, expectType } from 'tsd'
|
|
4
5
|
|
|
5
6
|
// Number schemas
|
|
6
7
|
build({
|
|
7
|
-
|
|
8
|
+
type: 'number'
|
|
8
9
|
})(25)
|
|
9
10
|
build({
|
|
10
|
-
|
|
11
|
+
type: 'integer'
|
|
11
12
|
})(-5)
|
|
12
13
|
build({
|
|
13
|
-
|
|
14
|
+
type: 'integer'
|
|
14
15
|
})(5n)
|
|
15
16
|
|
|
16
17
|
build({
|
|
17
|
-
|
|
18
|
+
type: 'number'
|
|
18
19
|
}, { rounding: 'ceil' })
|
|
19
20
|
build({
|
|
20
|
-
|
|
21
|
+
type: 'number'
|
|
21
22
|
}, { rounding: 'floor' })
|
|
22
23
|
build({
|
|
23
|
-
|
|
24
|
+
type: 'number'
|
|
24
25
|
}, { rounding: 'round' })
|
|
25
26
|
build({
|
|
26
|
-
|
|
27
|
+
type: 'number'
|
|
27
28
|
}, { rounding: 'trunc' })
|
|
28
29
|
expectError(build({
|
|
29
|
-
|
|
30
|
+
type: 'number'
|
|
30
31
|
}, { rounding: 'invalid' }))
|
|
31
32
|
|
|
32
33
|
// String schema
|
|
@@ -36,55 +37,55 @@ build({
|
|
|
36
37
|
|
|
37
38
|
// Boolean schema
|
|
38
39
|
build({
|
|
39
|
-
|
|
40
|
+
type: 'boolean'
|
|
40
41
|
})(true)
|
|
41
42
|
|
|
42
43
|
// Null schema
|
|
43
44
|
build({
|
|
44
|
-
|
|
45
|
+
type: 'null'
|
|
45
46
|
})(null)
|
|
46
47
|
|
|
47
48
|
// Array schemas
|
|
48
49
|
build({
|
|
49
|
-
|
|
50
|
-
|
|
50
|
+
type: 'array',
|
|
51
|
+
items: { type: 'number' }
|
|
51
52
|
})([25])
|
|
52
53
|
build({
|
|
53
|
-
|
|
54
|
-
|
|
54
|
+
type: 'array',
|
|
55
|
+
items: [{ type: 'string' }, { type: 'integer' }]
|
|
55
56
|
})(['hello', 42])
|
|
56
57
|
|
|
57
58
|
// Object schemas
|
|
58
59
|
build({
|
|
59
|
-
|
|
60
|
+
type: 'object'
|
|
60
61
|
})({})
|
|
61
62
|
build({
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
63
|
+
type: 'object',
|
|
64
|
+
properties: {
|
|
65
|
+
foo: { type: 'string' },
|
|
66
|
+
bar: { type: 'integer' }
|
|
67
|
+
},
|
|
68
|
+
required: ['foo'],
|
|
69
|
+
patternProperties: {
|
|
70
|
+
'baz*': { type: 'null' }
|
|
71
|
+
},
|
|
72
|
+
additionalProperties: {
|
|
73
|
+
type: 'boolean'
|
|
74
|
+
}
|
|
74
75
|
})({ foo: 'bar' })
|
|
75
76
|
build({
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
77
|
+
type: 'object',
|
|
78
|
+
properties: {
|
|
79
|
+
foo: { type: 'string' },
|
|
80
|
+
bar: { type: 'integer' }
|
|
81
|
+
},
|
|
82
|
+
required: ['foo'],
|
|
83
|
+
patternProperties: {
|
|
84
|
+
'baz*': { type: 'null' }
|
|
85
|
+
},
|
|
86
|
+
additionalProperties: {
|
|
87
|
+
type: 'boolean'
|
|
88
|
+
}
|
|
88
89
|
}, { rounding: 'floor' })({ foo: 'bar' })
|
|
89
90
|
|
|
90
91
|
// Reference schemas
|
|
@@ -113,7 +114,7 @@ build({
|
|
|
113
114
|
}
|
|
114
115
|
},
|
|
115
116
|
patternProperties: {
|
|
116
|
-
|
|
117
|
+
num: {
|
|
117
118
|
$ref: '#/definitions/num'
|
|
118
119
|
}
|
|
119
120
|
},
|
|
@@ -159,7 +160,7 @@ build({
|
|
|
159
160
|
})(new Date())
|
|
160
161
|
|
|
161
162
|
/*
|
|
162
|
-
This overload doesn't work yet -
|
|
163
|
+
This overload doesn't work yet -
|
|
163
164
|
TypeScript chooses the generic for the schema
|
|
164
165
|
before it chooses the overload for the options
|
|
165
166
|
parameter.
|
|
@@ -207,52 +208,52 @@ interface InferenceSchema {
|
|
|
207
208
|
}
|
|
208
209
|
|
|
209
210
|
const stringify3 = build({
|
|
210
|
-
type:
|
|
211
|
-
properties: { a: { type:
|
|
212
|
-
})
|
|
213
|
-
stringify3<InferenceSchema>({ id:
|
|
214
|
-
stringify3<InferenceSchema>({ a: 123, id:
|
|
215
|
-
expectError(stringify3<InferenceSchema>({ anotherOne:
|
|
216
|
-
expectError(stringify3<Schema>({ a:
|
|
211
|
+
type: 'object',
|
|
212
|
+
properties: { a: { type: 'string' } },
|
|
213
|
+
})
|
|
214
|
+
stringify3<InferenceSchema>({ id: '123' })
|
|
215
|
+
stringify3<InferenceSchema>({ a: 123, id: '123' })
|
|
216
|
+
expectError(stringify3<InferenceSchema>({ anotherOne: 'bar' }))
|
|
217
|
+
expectError(stringify3<Schema>({ a: 'bar' }))
|
|
217
218
|
|
|
218
219
|
// Without inference
|
|
219
220
|
const stringify4 = build({
|
|
220
|
-
type:
|
|
221
|
-
properties: { a: { type:
|
|
222
|
-
})
|
|
223
|
-
stringify4({ id:
|
|
224
|
-
stringify4({ a: 123, id:
|
|
225
|
-
stringify4({ anotherOne:
|
|
226
|
-
stringify4({ a:
|
|
221
|
+
type: 'object',
|
|
222
|
+
properties: { a: { type: 'string' } },
|
|
223
|
+
})
|
|
224
|
+
stringify4({ id: '123' })
|
|
225
|
+
stringify4({ a: 123, id: '123' })
|
|
226
|
+
stringify4({ anotherOne: 'bar' })
|
|
227
|
+
stringify4({ a: 'bar' })
|
|
227
228
|
|
|
228
229
|
// Without inference - string type
|
|
229
230
|
const stringify5 = build({
|
|
230
|
-
type:
|
|
231
|
-
})
|
|
232
|
-
stringify5(
|
|
233
|
-
expectError(stringify5({ id:
|
|
231
|
+
type: 'string',
|
|
232
|
+
})
|
|
233
|
+
stringify5('foo')
|
|
234
|
+
expectError(stringify5({ id: '123' }))
|
|
234
235
|
|
|
235
236
|
// Without inference - null type
|
|
236
237
|
const stringify6 = build({
|
|
237
|
-
type:
|
|
238
|
-
})
|
|
239
|
-
stringify6(null)
|
|
240
|
-
expectError(stringify6(
|
|
238
|
+
type: 'null',
|
|
239
|
+
})
|
|
240
|
+
stringify6(null)
|
|
241
|
+
expectError(stringify6('a string'))
|
|
241
242
|
|
|
242
243
|
// Without inference - boolean type
|
|
243
244
|
const stringify7 = build({
|
|
244
|
-
type:
|
|
245
|
-
})
|
|
246
|
-
stringify7(true)
|
|
247
|
-
expectError(stringify7(
|
|
245
|
+
type: 'boolean',
|
|
246
|
+
})
|
|
247
|
+
stringify7(true)
|
|
248
|
+
expectError(stringify7('a string'))
|
|
248
249
|
|
|
249
250
|
// largeArrayMechanism
|
|
250
251
|
|
|
251
|
-
build({}, { largeArrayMechanism: 'json-stringify'}
|
|
252
|
-
build({}, { largeArrayMechanism: 'default'}
|
|
253
|
-
expectError(build({} as Schema, { largeArrayMechanism: 'invalid'}
|
|
252
|
+
build({}, { largeArrayMechanism: 'json-stringify' })
|
|
253
|
+
build({}, { largeArrayMechanism: 'default' })
|
|
254
|
+
expectError(build({} as Schema, { largeArrayMechanism: 'invalid' }))
|
|
254
255
|
|
|
255
|
-
build({}, { largeArraySize: 2000 }
|
|
256
|
-
build({}, { largeArraySize: '2e4' }
|
|
257
|
-
build({}, { largeArraySize: 2n }
|
|
258
|
-
expectError(build({} as Schema, { largeArraySize: ['asdf']}
|
|
256
|
+
build({}, { largeArraySize: 2000 })
|
|
257
|
+
build({}, { largeArraySize: '2e4' })
|
|
258
|
+
build({}, { largeArraySize: 2n })
|
|
259
|
+
expectError(build({} as Schema, { largeArraySize: ['asdf'] }))
|