Skip to content
This repository was archived by the owner on May 11, 2021. It is now read-only.

Commit e18fc91

Browse files
authored
Merge pull request #11 from sevenwestmedia-labs/fix/unknown-types
Fixed build issues with recursive types and newer versions of TypeScript
2 parents 9d94420 + 3cc65ca commit e18fc91

File tree

12 files changed

+1541
-189
lines changed

12 files changed

+1541
-189
lines changed

.changeset/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Changesets
2+
3+
Hello and welcome! This folder has been automatically generated by `@changesets/cli`, a build tool that works
4+
with multi-package repos, or single-package repos to help you version and publish your code. You can
5+
find the full documentation for it [in our repository](https://github.com/changesets/changesets)
6+
7+
We have a quick list of common questions to get you started engaging with this project in
8+
[our documentation](https://github.com/changesets/changesets/blob/master/docs/common-questions.md)

.changeset/config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"$schema": "https://unpkg.com/@changesets/[email protected]/schema.json",
3+
"changelog": "@changesets/cli/changelog",
4+
"commit": false,
5+
"linked": [],
6+
"access": "public",
7+
"baseBranch": "master"
8+
}

.changeset/four-apricots-arrive.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'typescript-object-validator': minor
3+
---
4+
5+
Fixed build issues with recursive types and newer versions of TypeScript

.github/workflows/ci.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
name: CI
2+
on: [push, pull_request]
3+
jobs:
4+
run:
5+
name: Run
6+
runs-on: ubuntu-latest
7+
steps:
8+
- uses: actions/checkout@master
9+
10+
- name: Set Node.js 12.x
11+
uses: actions/setup-node@master
12+
with:
13+
node-version: 12.x
14+
15+
- name: yarn install
16+
run: yarn install
17+
18+
- name: Verify
19+
run: yarn verify

.github/workflows/release.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
name: Release
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
8+
jobs:
9+
release:
10+
name: Release
11+
runs-on: ubuntu-latest
12+
steps:
13+
- name: Checkout Repo
14+
uses: actions/checkout@master
15+
16+
- name: Setup Node.js 12.x
17+
uses: actions/setup-node@master
18+
with:
19+
node-version: 12.x
20+
21+
- name: Install Dependencies
22+
run: yarn
23+
24+
- name: Create Release Pull Request or Publish to npm
25+
uses: changesets/action@master
26+
with:
27+
# this expects you to have a script called release which does a build for your packages and calls changeset publish
28+
publish: yarn release
29+
env:
30+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
31+
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
node_modules
22
dist
33
.DS_Store
4+
yarn-error.log

.travis.yml

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

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
# Typescript Object Validator
22

3-
[![Build Status](https://travis-ci.com/sevenwestmedia-labs/typescript-object-validator.svg?branch=master)](https://travis-ci.com/sevenwestmedia-labs/typescript-object-validator) [![Greenkeeper badge](https://badges.greenkeeper.io/sevenwestmedia-labs/typescript-object-validator.svg)](https://greenkeeper.io/)
4-
53
A simple library for typescript projects to validating object shapes. You can use this package to declare the shape you'd like an object to align to. This is handy when you have a complex object (for example, an API response) and want to validate the object, AND have typescript recognize the shape.
64

75
## Basic Usage

package.json

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
{
22
"name": "typescript-object-validator",
3-
"version": "0.0.0",
3+
"version": "1.0.4",
44
"description": "TypeScript first object validator",
55
"main": "dist/cjs/index.js",
66
"module": "dist/esm/index.js",
7+
"types": "dist/cjs/index.d.ts",
78
"sideEffects": false,
89
"scripts": {
910
"prepack": "yarn build",
@@ -12,23 +13,28 @@
1213
"build:cjs": "tsc -p tsconfig.build.json --module commonjs --target es5 --outDir dist/cjs",
1314
"lint": "eslint --ext .js,.ts src",
1415
"test": "jest",
15-
"verify": "yarn tsc -p tsconfig.json && yarn test && yarn lint"
16+
"verify": "yarn tsc -p tsconfig.json && yarn test && yarn lint && yarn build && yarn tsd",
17+
"changeset": "changeset",
18+
"release": "yarn verify && changeset publish"
1619
},
1720
"author": "Jake Ginnivan",
1821
"license": "MIT",
1922
"devDependencies": {
20-
"@types/jest": "^24.0.18",
21-
"@typescript-eslint/eslint-plugin": "^2.3.2",
22-
"@typescript-eslint/parser": "^2.3.2",
23-
"eslint": "^6.5.1",
24-
"eslint-config-prettier": "^6.3.0",
25-
"eslint-config-wanews-base": "^2.0.2",
23+
"@changesets/cli": "^2.4.1",
24+
"@types/jest": "^24.9.0",
25+
"@typescript-eslint/eslint-plugin": "^2.17.0",
26+
"@typescript-eslint/parser": "^2.17.0",
27+
"eslint": "^6.8.0",
28+
"eslint-config-prettier": "^6.9.0",
29+
"eslint-config-wanews-base": "^2.1.1",
2630
"jest": "^24.9.0",
27-
"ts-jest": "^24.1.0",
31+
"ts-jest": "^24.3.0",
32+
"tsd": "^0.11.0",
2833
"tslib": "^1.10.0",
29-
"typescript": "^3.6.3"
34+
"typescript": "^3.7.5"
3035
},
3136
"peerDependencies": {
3237
"tslib": "^1.10.0"
33-
}
34-
}
38+
},
39+
"dependencies": {}
40+
}

src/index.ts

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ export type ValidatedType<
1616
: O extends Array<'boolean'>
1717
? boolean[] | undefined
1818
: O extends ObjectShape<O>
19-
? { [key in keyof O]: ValidatedType<O[key]> } | undefined
19+
? MapObjectShape<O> | undefined
2020
: O extends Array<infer U>
21-
? U extends ObjectShape<T>
22-
? Array<{ [key in keyof U]: ValidatedType<U[key]> }> | undefined
21+
? U extends ObjectShape<U>
22+
? Array<MapObjectShape<U>> | undefined
2323
: unknown
2424
: unknown
2525
: T extends 'unknown'
@@ -36,14 +36,18 @@ export type ValidatedType<
3636
? number[]
3737
: T extends Array<'boolean'>
3838
? boolean[]
39-
: T extends ObjectShape<object>
40-
? { [key in keyof T]: ValidatedType<T[key]> }
39+
: T extends ObjectShape<T>
40+
? MapObjectShape<T>
4141
: T extends Array<infer U>
42-
? U extends ObjectShape<object>
43-
? Array<{ [key in keyof U]: ValidatedType<U[key]> }>
42+
? U extends ObjectShape<U>
43+
? Array<MapObjectShape<U>>
4444
: unknown
4545
: unknown
4646

47+
type MapObjectShape<T extends ObjectShape<T>> = {
48+
[key in keyof T]: ValidatedType<T[key]>
49+
}
50+
4751
export interface ValidationOptions {
4852
/**
4953
* When the type should be an array, if a value is found, but would
@@ -85,10 +89,8 @@ export const validationTypes = {
8589
}
8690

8791
// We need this generic type so we can merge ObjectShapes and not lose info
88-
// eslint-disable-next-line @typescript-eslint/ban-ts-ignore
89-
// @ts-ignore
90-
export interface ObjectShape<T> {
91-
[key: string]: ValidationKeyType<T> | OptionalShape<T>
92+
export type ObjectShape<T extends {}> = {
93+
[key in keyof T]: ValidationKeyType<T[key]> | OptionalShape<T[key]>
9294
}
9395

9496
export type ValidationResult<
@@ -114,7 +116,7 @@ export function optional<T extends ValidationKeyType<T>>(
114116
}
115117
}
116118

117-
export function validateObjectShape<T extends ObjectShape<object>>(
119+
export function validateObjectShape<T extends ObjectShape<T>>(
118120
objectDescription: string,
119121
validationItem: unknown,
120122
expectedObjectShape: T,
@@ -140,7 +142,7 @@ export function validateObjectShape<T extends ObjectShape<object>>(
140142
Object.keys(expectedObjectShape).forEach(expectedKey => {
141143
try {
142144
const actualValue = validationItem[expectedKey]
143-
const expectedType = expectedObjectShape[expectedKey]
145+
const expectedType = (expectedObjectShape as any)[expectedKey]
144146

145147
if (isOptional(expectedType)) {
146148
if (actualValue === undefined) {

0 commit comments

Comments
 (0)