Skip to content

Commit

Permalink
[#501] Web: Add capability to do a runtime check of the type for the …
Browse files Browse the repository at this point in the history
…trails types.
  • Loading branch information
a-stacey committed Sep 10, 2019
1 parent 8e54c86 commit 654bfbc
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
2 changes: 1 addition & 1 deletion projects/web/src/components/submitTrail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as React from "react";
import { Link } from "react-router-dom";

import { myGlobals } from "../globals";
import { UnsignedTrailEntry, SignedTrailEntry } from "../trails";
import { UnsignedTrailEntry, SignedTrailEntry, isSignedTrailEntryArray} from "../trails";

import { AuthState } from "../auth";
import { Organisation } from "../org-registry";
Expand Down
53 changes: 51 additions & 2 deletions projects/web/src/trails.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { string } from "prop-types";
import { isArray } from "util";

export interface SignedTrailEntry extends UnsignedTrailEntry {
signature: string;
}

export interface UnsignedTrailEntry {
version: number;
Expand All @@ -8,6 +13,50 @@ export interface UnsignedTrailEntry {
previous_signatures : string[];
}

export interface SignedTrailEntry extends UnsignedTrailEntry {
signature: string;
// Its a real shame that we need to write this explicitly and effectively have to duplicate the type. It would be nice
// to be able to derrive this directly from the type and if this is possible this should be replaced with that
// implementation.
export function isSignedTrailEntryArray(value: any): value is SignedTrailEntry[] {
if (isArray(value)) {
return value.every(item => isSignedTrailEntry(item));
}

return false;
}

// Its a real shame that we need to write this explicitly and effectively have to duplicate the type. It would be nice
// to be able to derrive this directly from the type and if this is possible this should be replaced with that
// implementation.
function isSignedTrailEntry(value: any): value is SignedTrailEntry {
if (value as SignedTrailEntry) {
if (("signature" in value) &&
(typeof value.signature === "string")) {
return isUnsignedTrailEntry(value);
}
}

return false;
}

// Its a real shame that we need to write this explicitly and effectively have to duplicate the type. It would be nice
// to be able to derrive this directly from the type and if this is possible this should be replaced with that
// implementation.
function isUnsignedTrailEntry(value: any): value is UnsignedTrailEntry {
if (value as UnsignedTrailEntry){
if (("version" in value) &&
(typeof value.version === "number") &&
("timestamp" in value) &&
(typeof value.timestamp === "string") &&
("org" in value) &&
(typeof value.org === "string") &&
("event_id" in value) &&
(typeof value.event_id === "string") &&
("previous_signatures" in value) &&
(isArray(value.previous_signatures)) &&
(value.previous_signatures.every((signature: any) => typeof signature === "string"))) {
return true;
}
}

return false;
}

0 comments on commit 654bfbc

Please sign in to comment.