Skip to content

Commit 7a79234

Browse files
authored
nodenext compatibility (#564)
1 parent 3b492c2 commit 7a79234

File tree

6 files changed

+81
-70
lines changed

6 files changed

+81
-70
lines changed

index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -888,6 +888,8 @@ function buildValue (location, input) {
888888
}
889889

890890
module.exports = build
891+
module.exports.default = build
892+
module.exports.build = build
891893

892894
module.exports.validLargeArrayMechanisms = validLargeArrayMechanisms
893895

package.json

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
"version": "5.4.1",
44
"description": "Stringify your JSON at max speed",
55
"main": "index.js",
6-
"types": "index.d.ts",
6+
"types": "types/index.d.ts",
77
"scripts": {
88
"bench": "node ./benchmark/bench.js",
99
"bench:cmp": "node ./benchmark/bench-cmp-branch.js",
1010
"bench:cmp:ci": "node ./benchmark/bench-cmp-branch.js --ci",
1111
"benchmark": "node ./benchmark/bench-cmp-lib.js",
1212
"lint": "standard",
1313
"lint:fix": "standard --fix",
14-
"test:typescript": "tsc --project ./test/types/tsconfig.json && tsd",
14+
"test:typescript": "tsd",
1515
"test:unit": "tap -J test/*.test.js test/**/*.test.js",
1616
"test": "npm run test:unit && npm run test:typescript"
1717
},
@@ -46,7 +46,6 @@
4646
"standard": "^17.0.0",
4747
"tap": "^16.0.1",
4848
"tsd": "^0.24.1",
49-
"typescript": "^4.0.2",
5049
"webpack": "^5.40.0"
5150
},
5251
"dependencies": {
@@ -62,8 +61,5 @@
6261
"schema-validator.js"
6362
]
6463
},
65-
"runkitExampleFilename": "./examples/example.js",
66-
"tsd": {
67-
"directory": "test/types"
68-
}
64+
"runkitExampleFilename": "./examples/example.js"
6965
}

test/types/schema-inference.test-d.ts

Lines changed: 0 additions & 48 deletions
This file was deleted.

test/types/tsconfig.json

Lines changed: 0 additions & 13 deletions
This file was deleted.

index.d.ts renamed to types/index.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import Ajv, { Options as AjvOptions } from "ajv"
22

3+
type Build = typeof build
4+
35
declare namespace build {
46
interface BaseSchema {
57
/**
@@ -168,6 +170,12 @@ declare namespace build {
168170
*/
169171
mode?: 'debug' | 'standalone'
170172
}
173+
174+
export const validLargeArrayMechanisms: string[]
175+
export function restore (value: <TDoc extends object = object>(doc: TDoc) => string): ReturnType<Build>
176+
177+
export const build: Build
178+
export { build as default }
171179
}
172180

173181
interface DebugOption extends build.Options {

test/types/test.ts renamed to types/index.test-d.ts

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Ajv from 'ajv'
2-
import build, { Schema } from '../..'
2+
import build, { restore, Schema, validLargeArrayMechanisms } from '..'
3+
import { expectError, expectType } from 'tsd'
34

45
// Number schemas
56
const schema1: Schema = {
@@ -150,4 +151,69 @@ str = build(schema1, { debugMode: true }).code
150151
ajv = build(schema1, { debugMode: true }).ajv
151152
str = build(schema1, { mode: 'debug' }).code
152153
ajv = build(schema1, { mode: 'debug' }).ajv
153-
str = build(schema1, { mode: 'standalone' })
154+
str = build(schema1, { mode: 'standalone' })
155+
156+
const debugCompiled = build({
157+
title: 'default string',
158+
type: 'object',
159+
properties: {
160+
firstName: {
161+
type: 'string'
162+
}
163+
}
164+
}, { mode: 'debug' })
165+
expectType<ReturnType<typeof build>>(build.restore(debugCompiled))
166+
expectType<ReturnType<typeof build>>(restore(debugCompiled))
167+
168+
expectType<string[]>(build.validLargeArrayMechanisms)
169+
expectType<string[]>(validLargeArrayMechanisms)
170+
171+
/**
172+
* Schema inference
173+
*/
174+
175+
// With inference
176+
interface InferenceSchema {
177+
id: string;
178+
a?: number;
179+
}
180+
181+
const stringify3 = build({
182+
type: "object",
183+
properties: { a: { type: "string" } },
184+
});
185+
stringify3<InferenceSchema>({ id: "123" });
186+
stringify3<InferenceSchema>({ a: 123, id: "123" });
187+
expectError(stringify3<InferenceSchema>({ anotherOne: "bar" }));
188+
expectError(stringify3<Schema>({ a: "bar" }));
189+
190+
// Without inference
191+
const stringify4 = build({
192+
type: "object",
193+
properties: { a: { type: "string" } },
194+
});
195+
stringify4({ id: "123" });
196+
stringify4({ a: 123, id: "123" });
197+
stringify4({ anotherOne: "bar" });
198+
stringify4({ a: "bar" });
199+
200+
// Without inference - string type
201+
const stringify5 = build({
202+
type: "string",
203+
});
204+
stringify5("foo");
205+
expectError(stringify5({ id: "123" }));
206+
207+
// Without inference - null type
208+
const stringify6 = build({
209+
type: "null",
210+
});
211+
stringify6(null);
212+
expectError(stringify6("a string"));
213+
214+
// Without inference - boolean type
215+
const stringify7 = build({
216+
type: "boolean",
217+
});
218+
stringify7(true);
219+
expectError(stringify7("a string"));

0 commit comments

Comments
 (0)