Skip to content
This repository has been archived by the owner on Oct 1, 2019. It is now read-only.

Commit

Permalink
Replace tcomb-related libs with flow-runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
artkravchenko committed Apr 1, 2017
1 parent 48f57e6 commit 0056598
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 74 deletions.
13 changes: 12 additions & 1 deletion .babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,16 @@
["lodash"]
],
"retainLines": true,
"sourceMaps": "both"
"sourceMaps": "both",
"env": {
"development": {
"plugins": [
["flow-runtime", {
"annotate": false,
"assert": true,
"warn": true
}]
]
}
}
}
2 changes: 0 additions & 2 deletions .flowconfig
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,3 @@ esproposal.class_instance_fields=enable

suppress_type=$FlowIssue
suppress_type=$FlowFixMe

suppress_comment=\\(.\\|\n\\)*\\$FlowTcombIssue
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,9 @@
"babel-core": "~6.24.0",
"babel-eslint": "~7.1.0",
"babel-loader": "^6.2.4",
"babel-plugin-flow-runtime": "^0.10.0",
"babel-plugin-lodash": "^3.2.0",
"babel-plugin-react-transform": "^2.0.2",
"babel-plugin-tcomb": "^0.3.19",
"babel-preset-es2015": "^6.13.0",
"babel-preset-react": "^6.11.1",
"babel-preset-react-hmre": "^1.1.1",
Expand All @@ -147,6 +147,7 @@
"faker": "^3.0.1",
"file-loader": "^0.9.0",
"flow-bin": "~0.37.4",
"flow-runtime": "^0.10.0",
"istanbul": "~1.1.0-alpha.1",
"jsdom": "^7.2.1",
"json-loader": "^0.5.4",
Expand All @@ -166,7 +167,6 @@
"rosie": "^1.3.0",
"sinon": "^1.17.5",
"style-loader": "^0.13.0",
"tcomb": "^3.2.15",
"tmp": "~0.0.29",
"transform-loader": "^0.2.3",
"unexpected": "^10.11.1",
Expand Down
7 changes: 1 addition & 6 deletions src/api/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -520,12 +520,7 @@ export default class ApiClient
}

async createPost(type: PostType, data: PostDraftData): Promise<Post> {
if (process.env.NODE_ENV === 'development') {
// $FlowTcombIssue
data = PostDraftData.update(data, { type: { '$set': type } });
} else {
data.type = type;
}
data.type = type;
const response = await this.postJSON(`/api/v1/posts`, data);
return await response.json();
}
Expand Down
73 changes: 50 additions & 23 deletions src/definitions/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,44 +17,71 @@
@flow
*/
import type { $Refinement } from 'tcomb';
import t, { reify } from 'flow-runtime';
import type { Type } from 'flow-runtime';

export type Success = {
success: true
};

export const isInteger = (x: number): boolean => x % 1 === 0;
export const isInteger = (x: number) => {
if (x % 1 !== 0) {
return 'must be a valid integer';
}
};

export type Integer = number & $Refinement<typeof isInteger>;
export type Integer = number;
const Integer = (reify: Type<Integer>);
Integer.addConstraint(isInteger);

export const isValidEmail = (x: string): boolean => (
!!x.match(/^[a-z0-9!#$%&"'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/i)
);
export const isEmail = (x: string) => {
if (!x.match(/^[a-z0-9!#$%&"'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/i)) {
return 'must be a valid email address';
}
};

export type Email = string & $Refinement<typeof isValidEmail>;
export type Email = string;
const Email = (reify: Type<Email>);
Email.addConstraint(isEmail);

export const isValidDate = (x: string): boolean => (
new Date(x).toString() !== 'Invalid date'
);
export const isDate = (x: string): boolean => {
if (new Date(x).toString() === 'Invalid date') {
return 'must be a valid date string';
}
};

export type DateType = string & $Refinement<typeof isValidDate>;
export type DateType = string;
const DateType = (reify: Type<DateType>);
DateType.addConstraint(isDate);

export const isValidUrl = (x: string): boolean => (
!!x.match(RegExp(/^[a-z0-9_\.'/:-]+$/i))
);
export const isUrl = (x: string) => {
if (!x.match(RegExp(/^[a-z0-9_\.'/:-]+$/i))) {
return 'must be a valid URL';
}
};

export type Url = string & $Refinement<typeof isValidUrl>;
export type Url = string;
const Url = (reify: Type<Url>);
Url.addConstraint(isUrl);

export const isValidUrlNode = (x: Url): boolean => (
!x.match(RegExp(/(:|\/)/))
);
export const isUrlNode = (x: Url) => {
if (!x.match(RegExp(/(:|\/)/))) {
return 'must be a valid URL node';
}
};

export type UrlNode = Url & $Refinement<typeof isValidUrlNode>;
export type UrlNode = Url;
const UrlNode = (reify: Type<UrlNode>);
UrlNode.addConstraint(isUrlNode);

export const isValidUuid4 = (x: string): boolean => (
!!x.match(RegExp(/^[a-f0-9]{8}-?[a-f0-9]{4}-?4[a-f0-9]{3}-?[89ab][a-f0-9]{3}-?[a-f0-9]{12}$/i))
);
export const isUuid4 = (x: string) => {
if (!x.match(RegExp(/^[a-f0-9]{8}-?[a-f0-9]{4}-?4[a-f0-9]{3}-?[89ab][a-f0-9]{3}-?[a-f0-9]{12}$/i))) {
return 'must be a valid UUID v4 string';
}
};

export type Uuid4 = string & $Refinement<typeof isValidUuid4>;
export type Uuid4 = string;
const Uuid4 = (reify: Type<Uuid4>);
Uuid4.addConstraint(isUuid4);

export type Map<K, T> = { [key: K]: T };
23 changes: 0 additions & 23 deletions src/definitions/lib/tcomb.js

This file was deleted.

28 changes: 19 additions & 9 deletions src/definitions/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
@flow
*/
import type { $Refinement } from 'tcomb';
import t, { reify } from 'flow-runtime';
import type { Type } from 'flow-runtime';

import type { Email, DateType, Map, UrlNode, Uuid4 } from './common';
import type { Attachment } from './attachments';
import type { Geotag } from './geotags';
Expand All @@ -26,17 +28,25 @@ import type { School } from './schools';

export type UserId = Uuid4;

export const isValidPassword = (x: string): boolean => (
!!x.match(/^[\x20-\x7E]{8,}$/)
);
export const isPassword = (x: string) => {
if (!x.match(/^[\x20-\x7E]{8,}$/)) {
return 'must be a password';
}
};

export type Password = string & $Refinement<typeof isValidPassword>;
export type Password = string;
const Password = (reify: Type<Password>);
Password.addConstraint(isPassword);

export const isValidUsername = (x: string): boolean => (
!!x.match(/^(?!.*\.{2})[a-z0-9\-\_\'\.]+$/i)
);
export const isUsername = (x: string) => {
if (!x.match(/^(?!.*\.{2})[a-z0-9\-\_\'\.]+$/i)) {
return 'must be a username';
}
};

export type Username = string & $Refinement<typeof isValidUsername>;
export type Username = string;
const Username = (reify: Type<Username>);
Username.addConstraint(isUsername);

export type UserRoleTitle =
| 'Teacher or School Staff Member'
Expand Down
6 changes: 0 additions & 6 deletions src/scripts/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,6 @@ import { API_HOST } from '../config';
import ApiClient from '../api/client';
import { initState } from '../store';

/* to get rid of throwed errors by default */
if (process.env.NODE_ENV === 'development') {
const t = require('tcomb');
t.fail = msg => console.error('[tcomb] '.concat(msg)); // eslint-disable-line no-console
}

bluebird.longStackTraces();
window.Promise = bluebird;

Expand Down
3 changes: 1 addition & 2 deletions webpack.dev.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ module.exports = {
],
env: {
development: {
presets: ['react-hmre'],
plugins: ['tcomb']
presets: ['react-hmre']
}
}
}
Expand Down

0 comments on commit 0056598

Please sign in to comment.