Skip to content

Commit

Permalink
nodenext compatibility (#564)
Browse files Browse the repository at this point in the history
  • Loading branch information
Uzlopak authored Dec 5, 2022
1 parent 3b492c2 commit 7a79234
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 70 deletions.
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -888,6 +888,8 @@ function buildValue (location, input) {
}

module.exports = build
module.exports.default = build
module.exports.build = build

module.exports.validLargeArrayMechanisms = validLargeArrayMechanisms

Expand Down
10 changes: 3 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
"version": "5.4.1",
"description": "Stringify your JSON at max speed",
"main": "index.js",
"types": "index.d.ts",
"types": "types/index.d.ts",
"scripts": {
"bench": "node ./benchmark/bench.js",
"bench:cmp": "node ./benchmark/bench-cmp-branch.js",
"bench:cmp:ci": "node ./benchmark/bench-cmp-branch.js --ci",
"benchmark": "node ./benchmark/bench-cmp-lib.js",
"lint": "standard",
"lint:fix": "standard --fix",
"test:typescript": "tsc --project ./test/types/tsconfig.json && tsd",
"test:typescript": "tsd",
"test:unit": "tap -J test/*.test.js test/**/*.test.js",
"test": "npm run test:unit && npm run test:typescript"
},
Expand Down Expand Up @@ -46,7 +46,6 @@
"standard": "^17.0.0",
"tap": "^16.0.1",
"tsd": "^0.24.1",
"typescript": "^4.0.2",
"webpack": "^5.40.0"
},
"dependencies": {
Expand All @@ -62,8 +61,5 @@
"schema-validator.js"
]
},
"runkitExampleFilename": "./examples/example.js",
"tsd": {
"directory": "test/types"
}
"runkitExampleFilename": "./examples/example.js"
}
48 changes: 0 additions & 48 deletions test/types/schema-inference.test-d.ts

This file was deleted.

13 changes: 0 additions & 13 deletions test/types/tsconfig.json

This file was deleted.

8 changes: 8 additions & 0 deletions index.d.ts → types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import Ajv, { Options as AjvOptions } from "ajv"

type Build = typeof build

declare namespace build {
interface BaseSchema {
/**
Expand Down Expand Up @@ -168,6 +170,12 @@ declare namespace build {
*/
mode?: 'debug' | 'standalone'
}

export const validLargeArrayMechanisms: string[]
export function restore (value: <TDoc extends object = object>(doc: TDoc) => string): ReturnType<Build>

export const build: Build
export { build as default }
}

interface DebugOption extends build.Options {
Expand Down
70 changes: 68 additions & 2 deletions test/types/test.ts → types/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Ajv from 'ajv'
import build, { Schema } from '../..'
import build, { restore, Schema, validLargeArrayMechanisms } from '..'
import { expectError, expectType } from 'tsd'

// Number schemas
const schema1: Schema = {
Expand Down Expand Up @@ -150,4 +151,69 @@ str = build(schema1, { debugMode: true }).code
ajv = build(schema1, { debugMode: true }).ajv
str = build(schema1, { mode: 'debug' }).code
ajv = build(schema1, { mode: 'debug' }).ajv
str = build(schema1, { mode: 'standalone' })
str = build(schema1, { mode: 'standalone' })

const debugCompiled = build({
title: 'default string',
type: 'object',
properties: {
firstName: {
type: 'string'
}
}
}, { mode: 'debug' })
expectType<ReturnType<typeof build>>(build.restore(debugCompiled))
expectType<ReturnType<typeof build>>(restore(debugCompiled))

expectType<string[]>(build.validLargeArrayMechanisms)
expectType<string[]>(validLargeArrayMechanisms)

/**
* Schema inference
*/

// With inference
interface InferenceSchema {
id: string;
a?: number;
}

const stringify3 = build({
type: "object",
properties: { a: { type: "string" } },
});
stringify3<InferenceSchema>({ id: "123" });
stringify3<InferenceSchema>({ a: 123, id: "123" });
expectError(stringify3<InferenceSchema>({ anotherOne: "bar" }));
expectError(stringify3<Schema>({ a: "bar" }));

// Without inference
const stringify4 = build({
type: "object",
properties: { a: { type: "string" } },
});
stringify4({ id: "123" });
stringify4({ a: 123, id: "123" });
stringify4({ anotherOne: "bar" });
stringify4({ a: "bar" });

// Without inference - string type
const stringify5 = build({
type: "string",
});
stringify5("foo");
expectError(stringify5({ id: "123" }));

// Without inference - null type
const stringify6 = build({
type: "null",
});
stringify6(null);
expectError(stringify6("a string"));

// Without inference - boolean type
const stringify7 = build({
type: "boolean",
});
stringify7(true);
expectError(stringify7("a string"));

0 comments on commit 7a79234

Please sign in to comment.