diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml index 107e0c0..de5224f 100644 --- a/.github/workflows/nodejs.yml +++ b/.github/workflows/nodejs.yml @@ -23,3 +23,16 @@ jobs: - run: yarn lint - run: git submodule update --init --recursive - run: yarn run test + types: + name: 'Test types' + runs-on: ubuntu-latest + defaults: + run: + working-directory: test/types + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-node@v4 + with: + node-version: 20 + - run: yarn + - run: yarn test diff --git a/index.d.ts b/index.d.ts index cdf2e71..05682ae 100644 --- a/index.d.ts +++ b/index.d.ts @@ -15,8 +15,8 @@ type Schema = $id?: string $anchor?: string $ref?: string - definitions?: { [id: string]: Schema } - $defs?: { [id: string]: Schema } + definitions?: Partial<{ [id: string]: Schema }> + $defs?: Partial<{ [id: string]: Schema }> $recursiveRef?: string $recursiveAnchor?: boolean // generic @@ -61,15 +61,15 @@ type Schema = contentMediaType?: string contentSchema?: Schema // objects - properties?: { [id: string]: Schema } + properties?: Partial<{ [id: string]: Schema }> maxProperties?: number minProperties?: number additionalProperties?: Schema - patternProperties?: { [pattern: string]: Schema } + patternProperties?: Partial<{ [pattern: string]: Schema }> propertyNames?: Schema - dependencies?: { [id: string]: Array | Schema } - dependentRequired?: { [id: string]: Array } - dependentSchemas?: { [id: string]: Schema } + dependencies?: Partial<{ [id: string]: Array | Schema }> + dependentRequired?: Partial<{ [id: string]: Array }> + dependentSchemas?: Partial<{ [id: string]: Schema }> // see-through unevaluatedProperties?: Schema unevaluatedItems?: Schema diff --git a/package.json b/package.json index d4c0062..8764679 100644 --- a/package.json +++ b/package.json @@ -36,7 +36,8 @@ "test": "npm run test:raw | tap-spec", "test:raw": "npm run test:normal && npm run test:module", "test:module": "tape -r ./test/tools/test-module.js test/*.js test/regressions/*.js", - "test:normal": "tape test/*.js test/regressions/*.js" + "test:normal": "tape test/*.js test/regressions/*.js", + "test:types": "yarn --cwd test/types test" }, "dependencies": {}, "devDependencies": { diff --git a/test/types/package.json b/test/types/package.json new file mode 100644 index 0000000..755eb0c --- /dev/null +++ b/test/types/package.json @@ -0,0 +1,11 @@ +{ + "name": "type-tests", + "private": true, + "devDependencies": { + "@types/node": "^20.11.16", + "typescript": "^5.3.3" + }, + "scripts": { + "test": "tsc --noEmit" + } +} diff --git a/test/types/tests/index.ts b/test/types/tests/index.ts new file mode 100644 index 0000000..f639754 --- /dev/null +++ b/test/types/tests/index.ts @@ -0,0 +1,41 @@ +import { validator } from '../../../index' + +const schema = { + type: 'object', + allOf: [ + { + if: { + properties: { + propertyA: { + const: 1, + }, + }, + }, + then: { + properties: { + propertyB: { + const: null, + }, + }, + }, + }, + { + if: { + properties: { + propertyA: { + const: 2, + }, + }, + }, + then: { + properties: { + propertyC: { + const: null, + }, + }, + }, + }, + ], +} + +const myValidator = validator(schema) diff --git a/test/types/tsconfig.json b/test/types/tsconfig.json new file mode 100644 index 0000000..7c465a6 --- /dev/null +++ b/test/types/tsconfig.json @@ -0,0 +1,7 @@ +{ + "compilerOptions": { + "strict": true, + }, + "include": ["tests"], + "exclude": ["node_modules"] +} diff --git a/test/types/yarn.lock b/test/types/yarn.lock new file mode 100644 index 0000000..d3e3892 --- /dev/null +++ b/test/types/yarn.lock @@ -0,0 +1,20 @@ +# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. +# yarn lockfile v1 + + +"@types/node@^20.11.16": + version "20.11.16" + resolved "https://registry.yarnpkg.com/@types/node/-/node-20.11.16.tgz#4411f79411514eb8e2926f036c86c9f0e4ec6708" + integrity sha512-gKb0enTmRCzXSSUJDq6/sPcqrfCv2mkkG6Jt/clpn5eiCbKTY+SgZUxo+p8ZKMof5dCp9vHQUAB7wOUTod22wQ== + dependencies: + undici-types "~5.26.4" + +typescript@^5.3.3: + version "5.3.3" + resolved "https://registry.yarnpkg.com/typescript/-/typescript-5.3.3.tgz#b3ce6ba258e72e6305ba66f5c9b452aaee3ffe37" + integrity sha512-pXWcraxM0uxAS+tN0AG/BF2TyqmHO014Z070UsJ+pFvYuRSq8KH8DmWpnbXe0pEPDHXZV3FcAbJkijJ5oNEnWw== + +undici-types@~5.26.4: + version "5.26.5" + resolved "https://registry.yarnpkg.com/undici-types/-/undici-types-5.26.5.tgz#bcd539893d00b56e964fd2657a4866b221a65617" + integrity sha512-JlCMO+ehdEIKqlFxk6IfVoAUVmgz7cU7zD/h9XZ0qzeosSHmUJVOzSQvvYSYWXkFXC+IfLKSIffhv0sVZup6pA==