diff --git a/lib/permissions/minimally-encoded-thread-permissions-validators.js b/lib/permissions/minimally-encoded-thread-permissions-validators.js new file mode 100644 --- /dev/null +++ b/lib/permissions/minimally-encoded-thread-permissions-validators.js @@ -0,0 +1,78 @@ +// @flow +import t, { type TInterface } from 'tcomb'; + +import { + tHexEncodedPermissionsBitmask, + tHexEncodedRolePermission, +} from './minimally-encoded-thread-permissions.js'; +import type { + MinimallyEncodedMemberInfo, + MinimallyEncodedRawThreadInfo, + MinimallyEncodedRelativeMemberInfo, + MinimallyEncodedRoleInfo, + MinimallyEncodedThreadCurrentUserInfo, + MinimallyEncodedThreadInfo, +} from '../types/minimally-encoded-thread-permissions-types.js'; +import { + memberInfoValidator, + rawThreadInfoValidator, + roleInfoValidator, + threadCurrentUserInfoValidator, + threadInfoValidator, +} from '../types/thread-types.js'; +import { tBool, tID, tShape } from '../utils/validation-utils.js'; + +const minimallyEncodedRoleInfoValidator: TInterface = + tShape({ + ...roleInfoValidator.meta.props, + minimallyEncoded: tBool(true), + permissions: t.list(tHexEncodedRolePermission), + }); + +const minimallyEncodedThreadCurrentUserInfoValidator: TInterface = + tShape({ + ...threadCurrentUserInfoValidator.meta.props, + minimallyEncoded: tBool(true), + permissions: tHexEncodedPermissionsBitmask, + }); + +const minimallyEncodedMemberInfoValidator: TInterface = + tShape({ + ...memberInfoValidator.meta.props, + minimallyEncoded: tBool(true), + permissions: tHexEncodedPermissionsBitmask, + }); + +const minimallyEncodedRelativeMemberInfoValidator: TInterface = + tShape({ + ...minimallyEncodedMemberInfoValidator.meta.props, + username: t.maybe(t.String), + isViewer: t.Boolean, + }); + +const minimallyEncodedThreadInfoValidator: TInterface = + tShape({ + ...threadInfoValidator.meta.props, + minimallyEncoded: tBool(true), + members: t.list(minimallyEncodedRelativeMemberInfoValidator), + roles: t.dict(tID, minimallyEncodedRoleInfoValidator), + currentUser: minimallyEncodedThreadCurrentUserInfoValidator, + }); + +const minimallyEncodedRawThreadInfoValidator: TInterface = + tShape({ + ...rawThreadInfoValidator.meta.props, + minimallyEncoded: tBool(true), + members: t.list(minimallyEncodedMemberInfoValidator), + roles: t.dict(tID, minimallyEncodedRoleInfoValidator), + currentUser: minimallyEncodedThreadCurrentUserInfoValidator, + }); + +export { + minimallyEncodedRoleInfoValidator, + minimallyEncodedThreadCurrentUserInfoValidator, + minimallyEncodedMemberInfoValidator, + minimallyEncodedRelativeMemberInfoValidator, + minimallyEncodedThreadInfoValidator, + minimallyEncodedRawThreadInfoValidator, +}; diff --git a/lib/permissions/minimally-encoded-thread-permissions.test.js b/lib/permissions/minimally-encoded-thread-permissions.test.js --- a/lib/permissions/minimally-encoded-thread-permissions.test.js +++ b/lib/permissions/minimally-encoded-thread-permissions.test.js @@ -5,6 +5,12 @@ exampleRawThreadInfoA, expectedDecodedExampleRawThreadInfoA, } from './minimally-encoded-thread-permissions-test-data.js'; +import { + minimallyEncodedMemberInfoValidator, + minimallyEncodedRawThreadInfoValidator, + minimallyEncodedRoleInfoValidator, + minimallyEncodedThreadCurrentUserInfoValidator, +} from './minimally-encoded-thread-permissions-validators.js'; import { decodeRolePermissionBitmask, decodeThreadRolePermissionsBitmaskArray, @@ -15,10 +21,6 @@ threadRolePermissionsBlobToBitmaskArray, } from './minimally-encoded-thread-permissions.js'; import { - minimallyEncodedMemberInfoValidator, - minimallyEncodedThreadCurrentUserInfoValidator, - minimallyEncodedRawThreadInfoValidator, - minimallyEncodedRoleInfoValidator, minimallyEncodeRawThreadInfo, decodeMinimallyEncodedRawThreadInfo, } from '../types/minimally-encoded-thread-permissions-types.js'; diff --git a/lib/types/minimally-encoded-thread-permissions-types.js b/lib/types/minimally-encoded-thread-permissions-types.js --- a/lib/types/minimally-encoded-thread-permissions-types.js +++ b/lib/types/minimally-encoded-thread-permissions-types.js @@ -1,16 +1,7 @@ // @flow import _mapValues from 'lodash/fp/mapValues.js'; -import type { TInterface } from 'tcomb'; -import t from 'tcomb'; -import { - memberInfoValidator, - rawThreadInfoValidator, - roleInfoValidator, - threadCurrentUserInfoValidator, - threadInfoValidator, -} from './thread-types.js'; import type { MemberInfo, RawThreadInfo, @@ -24,10 +15,7 @@ permissionsToBitmaskHex, threadPermissionsFromBitmaskHex, threadRolePermissionsBlobToBitmaskArray, - tHexEncodedRolePermission, - tHexEncodedPermissionsBitmask, } from '../permissions/minimally-encoded-thread-permissions.js'; -import { tBool, tID, tShape } from '../utils/validation-utils.js'; export type MinimallyEncodedRoleInfo = $ReadOnly<{ ...RoleInfo, @@ -35,13 +23,6 @@ +permissions: $ReadOnlyArray, }>; -const minimallyEncodedRoleInfoValidator: TInterface = - tShape({ - ...roleInfoValidator.meta.props, - minimallyEncoded: tBool(true), - permissions: t.list(tHexEncodedRolePermission), - }); - const minimallyEncodeRoleInfo = ( roleInfo: RoleInfo, ): MinimallyEncodedRoleInfo => ({ @@ -68,13 +49,6 @@ +permissions: string, }>; -const minimallyEncodedThreadCurrentUserInfoValidator: TInterface = - tShape({ - ...threadCurrentUserInfoValidator.meta.props, - minimallyEncoded: tBool(true), - permissions: tHexEncodedPermissionsBitmask, - }); - const minimallyEncodeThreadCurrentUserInfo = ( threadCurrentUserInfo: ThreadCurrentUserInfo, ): MinimallyEncodedThreadCurrentUserInfo => ({ @@ -101,13 +75,6 @@ +permissions: string, }>; -const minimallyEncodedMemberInfoValidator: TInterface = - tShape({ - ...memberInfoValidator.meta.props, - minimallyEncoded: tBool(true), - permissions: tHexEncodedPermissionsBitmask, - }); - const minimallyEncodeMemberInfo = ( memberInfo: MemberInfo, ): MinimallyEncodedMemberInfo => ({ @@ -134,13 +101,6 @@ +isViewer: boolean, }>; -const minimallyEncodedRelativeMemberInfoValidator: TInterface = - tShape({ - ...minimallyEncodedMemberInfoValidator.meta.props, - username: t.maybe(t.String), - isViewer: t.Boolean, - }); - const minimallyEncodeRelativeMemberInfo = ( relativeMemberInfo: RelativeMemberInfo, ): MinimallyEncodedRelativeMemberInfo => ({ @@ -169,15 +129,6 @@ +currentUser: MinimallyEncodedThreadCurrentUserInfo, }>; -const minimallyEncodedRawThreadInfoValidator: TInterface = - tShape({ - ...rawThreadInfoValidator.meta.props, - minimallyEncoded: tBool(true), - members: t.list(minimallyEncodedMemberInfoValidator), - roles: t.dict(tID, minimallyEncodedRoleInfoValidator), - currentUser: minimallyEncodedThreadCurrentUserInfoValidator, - }); - const minimallyEncodeRawThreadInfo = ( rawThreadInfo: RawThreadInfo, ): MinimallyEncodedRawThreadInfo => { @@ -212,15 +163,6 @@ +currentUser: MinimallyEncodedThreadCurrentUserInfo, }>; -const minimallyEncodedThreadInfoValidator: TInterface = - tShape({ - ...threadInfoValidator.meta.props, - minimallyEncoded: tBool(true), - members: t.list(minimallyEncodedRelativeMemberInfoValidator), - roles: t.dict(tID, minimallyEncodedRoleInfoValidator), - currentUser: minimallyEncodedThreadCurrentUserInfoValidator, - }); - const minimallyEncodeThreadInfo = ( threadInfo: ThreadInfo, ): MinimallyEncodedThreadInfo => { @@ -248,22 +190,16 @@ }; export { - minimallyEncodedRoleInfoValidator, minimallyEncodeRoleInfo, decodeMinimallyEncodedRoleInfo, - minimallyEncodedThreadCurrentUserInfoValidator, minimallyEncodeThreadCurrentUserInfo, decodeMinimallyEncodedThreadCurrentUserInfo, - minimallyEncodedMemberInfoValidator, minimallyEncodeMemberInfo, decodeMinimallyEncodedMemberInfo, - minimallyEncodedRawThreadInfoValidator, minimallyEncodeRawThreadInfo, decodeMinimallyEncodedRawThreadInfo, - minimallyEncodedRelativeMemberInfoValidator, minimallyEncodeRelativeMemberInfo, decodeMinimallyEncodedRelativeMemberInfo, - minimallyEncodedThreadInfoValidator, minimallyEncodeThreadInfo, decodeMinimallyEncodedThreadInfo, };