This repository was archived by the owner on Aug 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
RFC: Use io-ts to validate user input. #65
Open
BenPope
wants to merge
8
commits into
master
Choose a base branch
from
io-ts-validation
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
449fca0
Use io-ts to validate user input.
52e4e11
Update io-ts, fp-ts, typescript
191c303
yarn upgrade --latest (except got which requires node >=10)
55ecaab
Remove babel
ab5e825
Review comments
46fdafd
Merge branch 'io-ts-validation' into cleanup-deps
BenPope 60567e3
Merge pull request #67 from pusher/cleanup-deps
BenPope 48c4b89
Convert more types to io-ts
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,17 +11,25 @@ import { | |
TokenWithExpiry, | ||
} from "@pusher/platform-node" | ||
|
||
import { getCurrentTimeInSeconds } from "./utils" | ||
import { | ||
getCurrentTimeInSeconds, | ||
validateProperties, | ||
} from "./utils" | ||
import packageJSON from "../package.json" | ||
import * as t from 'io-ts' | ||
|
||
const NonEmptyString = t.refinement(t.string, s => s.length !== 0, "NonEmptyString"); | ||
|
||
export interface AuthenticationOptions { | ||
userId: string | ||
authPayload?: AuthenticatePayload | ||
} | ||
|
||
export interface UserIdOptions { | ||
userId: string | ||
} | ||
const UserIdOptions = t.interface({ | ||
userId: NonEmptyString | ||
},"UserIdOptions"); | ||
type UserIdOptions = t.TypeOf<typeof UserIdOptions>; | ||
export { UserIdOptions }; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The use of the same name for two different things is confusing at a glance (though I know you mentioned the docs gave an example like this) |
||
|
||
export interface GetRoomOptions { | ||
roomId: string | ||
|
@@ -98,9 +106,14 @@ export interface GetUsersOptions { | |
limit?: number | ||
} | ||
|
||
export interface RemoveRoomRoleForUserOptions extends UserIdOptions { | ||
roomId: string | ||
} | ||
const RemoveRoomRoleForUserOptions = t.intersection([ | ||
UserIdOptions, | ||
t.interface({ | ||
roomId: NonEmptyString | ||
}) | ||
],"RemoveRoomRoleForUserOptions"); | ||
type RemoveRoomRoleForUserOptions = t.TypeOf<typeof RemoveRoomRoleForUserOptions>; | ||
export { RemoveRoomRoleForUserOptions }; | ||
|
||
export interface BasicAssignRoleToUserOptions { | ||
userId: string | ||
|
@@ -983,6 +996,7 @@ export default class Chatkit { | |
} | ||
|
||
removeRoomRoleForUser(options: RemoveRoomRoleForUserOptions): Promise<void> { | ||
validateProperties(options, RemoveRoomRoleForUserOptions) | ||
return this.authorizerInstance | ||
.request({ | ||
method: "DELETE", | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,9 @@ | ||
import { ThrowReporter } from "io-ts/lib/ThrowReporter"; | ||
import { Decoder } from "io-ts"; | ||
|
||
export const getCurrentTimeInSeconds = (): number => | ||
Math.floor(Date.now() / 1000) | ||
|
||
export function validateProperties<I,A>(options: I, decoder: Decoder<I,A>) { | ||
ThrowReporter.report(decoder.decode(options)) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import tape from "tape" | ||
|
||
import { validateProperties } from "../../target/src/utils.js" | ||
import { RemoveRoomRoleForUserOptions } from "../../target/src/chatkit.js" | ||
|
||
const TEST_TIMEOUT = 200 | ||
|
||
function test(name, f) { | ||
tape(name, t => { | ||
t.timeoutAfter(TEST_TIMEOUT) | ||
f(t) | ||
}) | ||
} | ||
|
||
test("validateProperties", t => { | ||
validateProperties({userId: "foo", roomId: "bar"}, RemoveRoomRoleForUserOptions) | ||
t.end() | ||
}) | ||
|
||
test("validatePropertiesUndefinedFails", t => { | ||
t.throws( | ||
() => validateProperties({user: "foo", roomId: "bar"}, RemoveRoomRoleForUserOptions), | ||
RegExp('.*Invalid value undefined supplied.*RemoveRoomRoleForUserOptions.*userId.*NonEmptyString.*')) | ||
t.end() | ||
}) | ||
|
||
test("validatePropertiesNullFails", t => { | ||
t.throws( | ||
() => validateProperties({userId: "foo", roomId: null}, RemoveRoomRoleForUserOptions), | ||
RegExp('.*Invalid value null supplied to.*RemoveRoomRoleForUserOptions.*roomId.*NonEmptyString.*')) | ||
t.end(); | ||
}) | ||
|
||
test("validatePropertiesEmptyFails", t => { | ||
t.throws( | ||
() => validateProperties({userId: "foo", roomId: ""}, RemoveRoomRoleForUserOptions), | ||
RegExp('.*Invalid value "" supplied to.*RemoveRoomRoleForUserOptions.*roomId.*NonEmptyString.*')) | ||
t.end(); | ||
}) | ||
|
||
test("validatePropertiesWrongTypeFails", t => { | ||
t.throws( | ||
() => validateProperties({userId: "foo", roomId: 42}, RemoveRoomRoleForUserOptions), | ||
RegExp('.*Invalid value 42 supplied to.*RemoveRoomRoleForUserOptions.*roomId.*NonEmptyString.*')) | ||
t.end(); | ||
}) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can pull this in from https://github.com/gcanti/io-ts-types#usage
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems to be more restrictive; it won't take an normal string.
error TS2322: Type 'string' is not assignable to type 'Branded<string, NonEmptyStringBrand>'. Type 'string' is not assignable to type 'Brand<NonEmptyStringBrand>'.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah I see. never mind then