Skip to content

Commit

Permalink
simplify validation code
Browse files Browse the repository at this point in the history
  • Loading branch information
gwyneplaine committed Apr 16, 2021
1 parent d70d58a commit 445b080
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 28 deletions.
4 changes: 2 additions & 2 deletions packages-next/fields/src/types/image/views/Field.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { jsx, Stack, useTheme, Text } from '@keystone-ui/core';
import { useToasts } from '@keystone-ui/toast';
import { TextInput } from '@keystone-ui/fields';
import { isValidImageRef } from '@keystone-next/utils-legacy';
import { parseImageRef } from '@keystone-next/utils-legacy';
import copy from 'copy-to-clipboard';
import bytes from 'bytes';
import { ReactNode, useEffect, useMemo, useRef, useState } from 'react';
Expand Down Expand Up @@ -336,7 +336,7 @@ export function Field({
}

export function validateRef({ ref }: { ref: string }) {
if (!isValidImageRef(ref)) {
if (!parseImageRef(ref)) {
return 'Invalid ref';
}
}
Expand Down
11 changes: 10 additions & 1 deletion packages-next/keystone/src/lib/createImagesContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,16 @@ export function createImagesContext(config?: KeystoneImagesConfig): ImagesContex
throw new Error('Image not found');
},
getDataFromRef: async ref => {
const { mode, id, extension } = parseImageRef(ref);
const throwInvalidRefError = () => {
throw new Error('Invalid image reference');
};
if (!parseImageRef(ref)) throwInvalidRefError();

const { mode, id, extension } = parseImageRef(ref) as {
mode: ImageMode;
id: string;
extension: ImageExtension;
};

if (isLocal(mode)) {
const buffer = await fs.readFile(path.join(storagePath, `${id}.${extension}`));
Expand Down
34 changes: 9 additions & 25 deletions packages/utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,32 +273,16 @@ export const isValidImageRef = (ref: string): boolean => {

export const SUPPORTED_IMAGE_EXTENSIONS = ['jpg', 'png', 'webp', 'gif'];

const isValidImageExtension = (extension: string): boolean =>
SUPPORTED_IMAGE_EXTENSIONS.includes(extension);

export const parseImageRef = (
ref: string
): { mode: ImageMode; id: string; extension: ImageExtension } => {
const throwInvalidRefError = () => {
throw new Error('Invalid image reference');
};

if (!isValidImageRef(ref)) {
throwInvalidRefError();
}

const [__, mode, id, ext] = ref.match(REFREGEX) as RegExpMatchArray;

// const [mode, idAndExt] = ref.split(':');
// const [id, ext] = idAndExt.split('.');

if (!isValidImageExtension(ext)) {
throwInvalidRefError();
): { mode: ImageMode; id: string; extension: ImageExtension } | undefined => {
if (ref.match(REFREGEX)) {
const [__, mode, id, ext] = ref.match(REFREGEX) as RegExpMatchArray;
return {
mode: mode as ImageMode,
id,
extension: ext as ImageExtension,
};
}

return {
mode: mode as ImageMode,
id,
extension: ext as ImageExtension,
};
return undefined;
};

0 comments on commit 445b080

Please sign in to comment.