Changeset View
Changeset View
Standalone View
Standalone View
lib/utils/validation-utils.js
Show All 9 Lines | import type { | ||||
TUnion, | TUnion, | ||||
} from 'tcomb'; | } from 'tcomb'; | ||||
import { | import { | ||||
validEmailRegex, | validEmailRegex, | ||||
oldValidUsernameRegex, | oldValidUsernameRegex, | ||||
validHexColorRegex, | validHexColorRegex, | ||||
} from '../shared/account-utils.js'; | } from '../shared/account-utils.js'; | ||||
import type { PlatformDetails } from '../types/device-types'; | |||||
import type { | |||||
MediaMessageServerDBContent, | |||||
PhotoMessageServerDBContent, | |||||
VideoMessageServerDBContent, | |||||
} from '../types/messages/media'; | |||||
function tBool(value: boolean): TIrreducible<boolean> { | function tBool(value: boolean): TIrreducible<boolean> { | ||||
return t.irreducible('literal bool', x => x === value); | return t.irreducible('literal bool', x => x === value); | ||||
} | } | ||||
function tString(value: string): TIrreducible<string> { | function tString(value: string): TIrreducible<string> { | ||||
return t.irreducible('literal string', x => x === value); | return t.irreducible('literal string', x => x === value); | ||||
} | } | ||||
function tNumber(value: number): TIrreducible<number> { | function tNumber(value: number): TIrreducible<number> { | ||||
return t.irreducible('literal number', x => x === value); | return t.irreducible('literal number', x => x === value); | ||||
} | } | ||||
function tShape(spec: TStructProps): TInterface { | function tShape<T>(spec: TStructProps<T>): TInterface<T> { | ||||
return t.interface(spec, { strict: true }); | return t.interface(spec, { strict: true }); | ||||
} | } | ||||
type TRegex = TRefinement<string>; | type TRegex = TRefinement<string>; | ||||
function tRegex(regex: RegExp): TRegex { | function tRegex(regex: RegExp): TRegex { | ||||
return t.refinement(t.String, val => regex.test(val)); | return t.refinement(t.String, val => regex.test(val)); | ||||
} | } | ||||
Show All 13 Lines | |||||
const tPlatform: TEnums = t.enums.of([ | const tPlatform: TEnums = t.enums.of([ | ||||
'ios', | 'ios', | ||||
'android', | 'android', | ||||
'web', | 'web', | ||||
'windows', | 'windows', | ||||
'macos', | 'macos', | ||||
]); | ]); | ||||
const tDeviceType: TEnums = t.enums.of(['ios', 'android']); | const tDeviceType: TEnums = t.enums.of(['ios', 'android']); | ||||
const tPlatformDetails: TInterface = tShape({ | const tPlatformDetails: TInterface<PlatformDetails> = tShape({ | ||||
platform: tPlatform, | platform: tPlatform, | ||||
codeVersion: t.maybe(t.Number), | codeVersion: t.maybe(t.Number), | ||||
stateVersion: t.maybe(t.Number), | stateVersion: t.maybe(t.Number), | ||||
}); | }); | ||||
const tPassword: TRefinement<string> = t.refinement( | const tPassword: TRefinement<string> = t.refinement( | ||||
t.String, | t.String, | ||||
(password: string) => !!password, | (password: string) => !!password, | ||||
); | ); | ||||
const tCookie: TRegex = tRegex(/^(user|anonymous)=[0-9]+:[0-9a-f]+$/); | const tCookie: TRegex = tRegex(/^(user|anonymous)=[0-9]+:[0-9a-f]+$/); | ||||
const tEmail: TRegex = tRegex(validEmailRegex); | const tEmail: TRegex = tRegex(validEmailRegex); | ||||
const tOldValidUsername: TRegex = tRegex(oldValidUsernameRegex); | const tOldValidUsername: TRegex = tRegex(oldValidUsernameRegex); | ||||
const tID: TRefinement<string> = t.refinement(t.String, (id: string) => !!id); | const tID: TRefinement<string> = t.refinement(t.String, (id: string) => !!id); | ||||
const tMediaMessagePhoto: TInterface = tShape({ | const tMediaMessagePhoto: TInterface<PhotoMessageServerDBContent> = tShape({ | ||||
type: tString('photo'), | type: tString('photo'), | ||||
uploadID: t.String, | uploadID: t.String, | ||||
}); | }); | ||||
const tMediaMessageVideo: TInterface = tShape({ | const tMediaMessageVideo: TInterface<VideoMessageServerDBContent> = tShape({ | ||||
type: tString('video'), | type: tString('video'), | ||||
uploadID: t.String, | uploadID: t.String, | ||||
thumbnailUploadID: t.String, | thumbnailUploadID: t.String, | ||||
}); | }); | ||||
const tMediaMessageMedia: TUnion<TInterface> = t.union([ | const tMediaMessageMedia: TUnion<MediaMessageServerDBContent> = t.union([ | ||||
tMediaMessagePhoto, | tMediaMessagePhoto, | ||||
tMediaMessageVideo, | tMediaMessageVideo, | ||||
]); | ]); | ||||
export { | export { | ||||
tBool, | tBool, | ||||
tString, | tString, | ||||
tNumber, | tNumber, | ||||
Show All 17 Lines |