fast-json-stringify 5.4.1 → 5.6.0

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.
@@ -1,5 +1,6 @@
1
1
  import Ajv from 'ajv'
2
- import build, { Schema } from '../..'
2
+ import build, { restore, Schema, validLargeArrayMechanisms } from '..'
3
+ import { expectError, expectType } from 'tsd'
3
4
 
4
5
  // Number schemas
5
6
  const schema1: Schema = {
@@ -12,6 +13,12 @@ const schema2: Schema = {
12
13
  build(schema1)(25)
13
14
  build(schema2)(-5)
14
15
 
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' }))
21
+
15
22
  // String schema
16
23
  const schema3: Schema = {
17
24
  type: 'string'
@@ -150,4 +157,80 @@ str = build(schema1, { debugMode: true }).code
150
157
  ajv = build(schema1, { debugMode: true }).ajv
151
158
  str = build(schema1, { mode: 'debug' }).code
152
159
  ajv = build(schema1, { mode: 'debug' }).ajv
153
- str = build(schema1, { mode: 'standalone' })
160
+ str = build(schema1, { mode: 'standalone' })
161
+
162
+ const debugCompiled = build({
163
+ title: 'default string',
164
+ type: 'object',
165
+ properties: {
166
+ firstName: {
167
+ type: 'string'
168
+ }
169
+ }
170
+ }, { mode: 'debug' })
171
+ expectType<ReturnType<typeof build>>(build.restore(debugCompiled))
172
+ expectType<ReturnType<typeof build>>(restore(debugCompiled))
173
+
174
+ expectType<string[]>(build.validLargeArrayMechanisms)
175
+ expectType<string[]>(validLargeArrayMechanisms)
176
+
177
+ /**
178
+ * Schema inference
179
+ */
180
+
181
+ // With inference
182
+ interface InferenceSchema {
183
+ id: string;
184
+ a?: number;
185
+ }
186
+
187
+ const stringify3 = build({
188
+ type: "object",
189
+ properties: { a: { type: "string" } },
190
+ });
191
+ stringify3<InferenceSchema>({ id: "123" });
192
+ stringify3<InferenceSchema>({ a: 123, id: "123" });
193
+ expectError(stringify3<InferenceSchema>({ anotherOne: "bar" }));
194
+ expectError(stringify3<Schema>({ a: "bar" }));
195
+
196
+ // Without inference
197
+ const stringify4 = build({
198
+ type: "object",
199
+ properties: { a: { type: "string" } },
200
+ });
201
+ stringify4({ id: "123" });
202
+ stringify4({ a: 123, id: "123" });
203
+ stringify4({ anotherOne: "bar" });
204
+ stringify4({ a: "bar" });
205
+
206
+ // Without inference - string type
207
+ const stringify5 = build({
208
+ type: "string",
209
+ });
210
+ stringify5("foo");
211
+ expectError(stringify5({ id: "123" }));
212
+
213
+ // Without inference - null type
214
+ const stringify6 = build({
215
+ type: "null",
216
+ });
217
+ stringify6(null);
218
+ expectError(stringify6("a string"));
219
+
220
+ // Without inference - boolean type
221
+ const stringify7 = build({
222
+ type: "boolean",
223
+ });
224
+ stringify7(true);
225
+ expectError(stringify7("a string"));
226
+
227
+ // largeArrayMechanism
228
+
229
+ build({}, { largeArrayMechanism: 'json-stringify'} )
230
+ build({}, { largeArrayMechanism: 'default'} )
231
+ expectError(build({} as Schema, { largeArrayMechanism: 'invalid'} ))
232
+
233
+ build({}, { largeArraySize: 2000 } )
234
+ build({}, { largeArraySize: '2e4' } )
235
+ build({}, { largeArraySize: 2n } )
236
+ expectError(build({} as Schema, { largeArraySize: ['asdf']} ))
@@ -1,48 +0,0 @@
1
- import { expectError } from "tsd";
2
- import build from "../..";
3
-
4
- // With inference
5
- interface Schema {
6
- id: string;
7
- a?: number;
8
- }
9
-
10
- const stringify3 = build({
11
- type: "object",
12
- properties: { a: { type: "string" } },
13
- });
14
- stringify3<Schema>({ id: "123" });
15
- stringify3<Schema>({ a: 123, id: "123" });
16
- expectError(stringify3<Schema>({ anotherOne: "bar" }));
17
- expectError(stringify3<Schema>({ a: "bar" }));
18
-
19
- // Without inference
20
- const stringify4 = build({
21
- type: "object",
22
- properties: { a: { type: "string" } },
23
- });
24
- stringify4({ id: "123" });
25
- stringify4({ a: 123, id: "123" });
26
- stringify4({ anotherOne: "bar" });
27
- stringify4({ a: "bar" });
28
-
29
- // Without inference - string type
30
- const stringify5 = build({
31
- type: "string",
32
- });
33
- stringify5("foo");
34
- expectError(stringify5({ id: "123" }));
35
-
36
- // Without inference - null type
37
- const stringify6 = build({
38
- type: "null",
39
- });
40
- stringify6(null);
41
- expectError(stringify6("a string"));
42
-
43
- // Without inference - boolean type
44
- const stringify7 = build({
45
- type: "boolean",
46
- });
47
- stringify7(true);
48
- expectError(stringify7("a string"));
@@ -1,13 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "esModuleInterop": true,
4
- "noEmit": true,
5
- "rootDir": "../..",
6
- "strict": true,
7
- "target": "ES2015",
8
- "moduleResolution": "node"
9
- },
10
- "include": [
11
- "./test.ts"
12
- ]
13
- }