Skip to content

Commit 4adf1ed

Browse files
authored
fix: relax type on object properties (#180)
* fix: loosen types * eol * chore: add node types * chore: simplify script
1 parent f28dd6f commit 4adf1ed

File tree

7 files changed

+101
-8
lines changed

7 files changed

+101
-8
lines changed

.github/workflows/nodejs.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,16 @@ jobs:
2323
- run: yarn lint
2424
- run: git submodule update --init --recursive
2525
- run: yarn run test
26+
types:
27+
name: 'Test types'
28+
runs-on: ubuntu-latest
29+
defaults:
30+
run:
31+
working-directory: test/types
32+
steps:
33+
- uses: actions/checkout@v4
34+
- uses: actions/setup-node@v4
35+
with:
36+
node-version: 20
37+
- run: yarn
38+
- run: yarn test

index.d.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ type Schema =
1515
$id?: string
1616
$anchor?: string
1717
$ref?: string
18-
definitions?: { [id: string]: Schema }
19-
$defs?: { [id: string]: Schema }
18+
definitions?: Partial<{ [id: string]: Schema }>
19+
$defs?: Partial<{ [id: string]: Schema }>
2020
$recursiveRef?: string
2121
$recursiveAnchor?: boolean
2222
// generic
@@ -61,15 +61,15 @@ type Schema =
6161
contentMediaType?: string
6262
contentSchema?: Schema
6363
// objects
64-
properties?: { [id: string]: Schema }
64+
properties?: Partial<{ [id: string]: Schema }>
6565
maxProperties?: number
6666
minProperties?: number
6767
additionalProperties?: Schema
68-
patternProperties?: { [pattern: string]: Schema }
68+
patternProperties?: Partial<{ [pattern: string]: Schema }>
6969
propertyNames?: Schema
70-
dependencies?: { [id: string]: Array<string> | Schema }
71-
dependentRequired?: { [id: string]: Array<string> }
72-
dependentSchemas?: { [id: string]: Schema }
70+
dependencies?: Partial<{ [id: string]: Array<string> | Schema }>
71+
dependentRequired?: Partial<{ [id: string]: Array<string> }>
72+
dependentSchemas?: Partial<{ [id: string]: Schema }>
7373
// see-through
7474
unevaluatedProperties?: Schema
7575
unevaluatedItems?: Schema

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@
3636
"test": "npm run test:raw | tap-spec",
3737
"test:raw": "npm run test:normal && npm run test:module",
3838
"test:module": "tape -r ./test/tools/test-module.js test/*.js test/regressions/*.js",
39-
"test:normal": "tape test/*.js test/regressions/*.js"
39+
"test:normal": "tape test/*.js test/regressions/*.js",
40+
"test:types": "yarn --cwd test/types test"
4041
},
4142
"dependencies": {},
4243
"devDependencies": {

test/types/package.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "type-tests",
3+
"private": true,
4+
"devDependencies": {
5+
"@types/node": "^20.11.16",
6+
"typescript": "^5.3.3"
7+
},
8+
"scripts": {
9+
"test": "tsc --noEmit"
10+
}
11+
}

test/types/tests/index.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { validator } from '../../../index'
2+
3+
const schema = {
4+
type: 'object',
5+
allOf: [
6+
{
7+
if: {
8+
properties: {
9+
propertyA: {
10+
const: 1,
11+
},
12+
},
13+
},
14+
then: {
15+
properties: {
16+
propertyB: {
17+
const: null,
18+
},
19+
},
20+
},
21+
},
22+
{
23+
if: {
24+
properties: {
25+
propertyA: {
26+
const: 2,
27+
},
28+
},
29+
},
30+
then: {
31+
properties: {
32+
propertyC: {
33+
const: null,
34+
},
35+
},
36+
},
37+
},
38+
],
39+
}
40+
41+
const myValidator = validator(schema)

test/types/tsconfig.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"compilerOptions": {
3+
"strict": true,
4+
},
5+
"include": ["tests"],
6+
"exclude": ["node_modules"]
7+
}

test/types/yarn.lock

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
2+
# yarn lockfile v1
3+
4+
5+
"@types/node@^20.11.16":
6+
version "20.11.16"
7+
resolved "https://registry.yarnpkg.com/@types/node/-/node-20.11.16.tgz#4411f79411514eb8e2926f036c86c9f0e4ec6708"
8+
integrity sha512-gKb0enTmRCzXSSUJDq6/sPcqrfCv2mkkG6Jt/clpn5eiCbKTY+SgZUxo+p8ZKMof5dCp9vHQUAB7wOUTod22wQ==
9+
dependencies:
10+
undici-types "~5.26.4"
11+
12+
typescript@^5.3.3:
13+
version "5.3.3"
14+
resolved "https://registry.yarnpkg.com/typescript/-/typescript-5.3.3.tgz#b3ce6ba258e72e6305ba66f5c9b452aaee3ffe37"
15+
integrity sha512-pXWcraxM0uxAS+tN0AG/BF2TyqmHO014Z070UsJ+pFvYuRSq8KH8DmWpnbXe0pEPDHXZV3FcAbJkijJ5oNEnWw==
16+
17+
undici-types@~5.26.4:
18+
version "5.26.5"
19+
resolved "https://registry.yarnpkg.com/undici-types/-/undici-types-5.26.5.tgz#bcd539893d00b56e964fd2657a4866b221a65617"
20+
integrity sha512-JlCMO+ehdEIKqlFxk6IfVoAUVmgz7cU7zD/h9XZ0qzeosSHmUJVOzSQvvYSYWXkFXC+IfLKSIffhv0sVZup6pA==

0 commit comments

Comments
 (0)