diff --git a/lib/types/avatar-types.js b/lib/types/avatar-types.js --- a/lib/types/avatar-types.js +++ b/lib/types/avatar-types.js @@ -19,3 +19,9 @@ | EmojiAvatarDBContent | ImageAvatarDBContent | ENSAvatarDBContent; + +export type UpdateUserAvatarRemoveRequest = { +type: 'remove' }; + +export type UpdateUserAvatarRequest = + | AvatarDBContent + | UpdateUserAvatarRemoveRequest; diff --git a/lib/utils/avatar-utils.js b/lib/utils/avatar-utils.js new file mode 100644 --- /dev/null +++ b/lib/utils/avatar-utils.js @@ -0,0 +1,41 @@ +// @flow + +import t, { TInterface, TUnion } from 'tcomb'; + +import { tRegex, tShape, tString } from './validation-utils.js'; +import { validHexColorRegex } from '../shared/account-utils.js'; +import { onlyOneEmojiRegex } from '../shared/emojis.js'; + +const emojiAvatarDBContentValidator: TInterface = tShape({ + type: tString('emoji'), + emoji: tRegex(onlyOneEmojiRegex), + color: tRegex(validHexColorRegex), +}); + +const imageAvatarDBContentValidator: TInterface = tShape({ + type: tString('image'), + uploadID: t.String, +}); + +const ensAvatarDBContentValidator: TInterface = tShape({ + type: tString('ens'), +}); + +const updateUserAvatarRemoveRequestValidator: TInterface = tShape({ + type: tString('remove'), +}); + +const updateUserAvatarRequestValidator: TUnion = t.union([ + emojiAvatarDBContentValidator, + imageAvatarDBContentValidator, + ensAvatarDBContentValidator, + updateUserAvatarRemoveRequestValidator, +]); + +export { + emojiAvatarDBContentValidator, + imageAvatarDBContentValidator, + ensAvatarDBContentValidator, + updateUserAvatarRemoveRequestValidator, + updateUserAvatarRequestValidator, +}; diff --git a/lib/utils/avatar-utils.test.js b/lib/utils/avatar-utils.test.js new file mode 100644 --- /dev/null +++ b/lib/utils/avatar-utils.test.js @@ -0,0 +1,122 @@ +// @flow + +import { + emojiAvatarDBContentValidator, + ensAvatarDBContentValidator, + imageAvatarDBContentValidator, + updateUserAvatarRequestValidator, +} from './avatar-utils.js'; + +describe('emojiAvatarDBContentValidator', () => { + it('should succeed for valid input', () => { + expect( + emojiAvatarDBContentValidator.is({ + type: 'emoji', + emoji: '👍', + color: '4b87aa', + }), + ).toBe(true); + }); + + it('should fail if type is incorrect', () => { + expect( + emojiAvatarDBContentValidator.is({ + type: 'memoji', + emoji: '👍', + color: '4b87aa', + }), + ).toBe(false); + }); + + it(`should fail if emoji isn't valid`, () => { + expect( + emojiAvatarDBContentValidator.is({ + type: 'emoji', + emoji: ':)', + color: '4b87aa', + }), + ).toBe(false); + }); + + it(`should fail if color isn't valid`, () => { + expect( + emojiAvatarDBContentValidator.is({ + type: 'emoji', + emoji: '👍', + color: '#4b87aa', + }), + ).toBe(false); + expect( + emojiAvatarDBContentValidator.is({ + type: 'emoji', + emoji: '👍', + color: '#4b87aa00', + }), + ).toBe(false); + }); +}); + +describe('imageAvatarDBContentValidator', () => { + it('should succeed for valid input', () => { + expect( + imageAvatarDBContentValidator.is({ + type: 'image', + uploadID: '123456', + }), + ).toBe(true); + }); + + it('should fail if type is incorrect', () => { + expect( + imageAvatarDBContentValidator.is({ + type: 'emoji', + uploadID: '123456', + }), + ).toBe(false); + }); + + it('should fail if uploadID is incorrect type', () => { + expect( + imageAvatarDBContentValidator.is({ + type: 'image', + uploadID: 123456, + }), + ).toBe(false); + }); +}); + +describe('ensAvatarDBContentValidator', () => { + it('should succeed for valid input', () => { + expect( + ensAvatarDBContentValidator.is({ + type: 'ens', + }), + ).toBe(true); + }); + + it('should fail for incorrect type', () => { + expect( + ensAvatarDBContentValidator.is({ + type: 'emoji', + }), + ).toBe(false); + }); +}); + +describe('updateUserAvatarRemoveRequestValidator', () => { + it('should succeed for valid input', () => { + expect( + updateUserAvatarRequestValidator.is({ + type: 'remove', + }), + ).toBe(true); + }); + + it('should fail for incorrect type', () => { + expect( + updateUserAvatarRequestValidator.is({ + type: 'emoji', + }), + ).toBe(false); + }); +});