Skip to content

Commit

Permalink
Remove isRequired and defaultValue from some fields (#6513)
Browse files Browse the repository at this point in the history
  • Loading branch information
emmatown authored Sep 9, 2021
1 parent bf58744 commit 4048991
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 50 deletions.
5 changes: 5 additions & 0 deletions .changeset/forty-jobs-grow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@keystone-next/cloudinary': major
---

Removed `isRequired` and `defaultValue` from the `cloudinaryImage` field. If you were using these options, the same behaviour can be re-created with the `validateInput` and `resolveInput` hooks respectively.
5 changes: 5 additions & 0 deletions .changeset/real-jars-buy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@keystone-next/keystone': major
---

Removed `isRequired` and `defaultValue` from the `image` and `file` fields. If you were using these options, the same behaviour can be re-created with the `validateInput` and `resolveInput` hooks respectively.
18 changes: 2 additions & 16 deletions docs/pages/docs/apis/fields.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -621,10 +621,6 @@ A `file` field represents a file of any type.

See [`config.files`](./config#files) for details on how to configure your Keystone system with support for the `file` field type.

Options:

- `isRequired` (default: `false`): If `true` then this field can never be set to `null`.

```typescript
import { config, createSchema, list } from '@keystone-next/keystone';
import { file } from '@keystone-next/keystone/fields';
Expand All @@ -633,9 +629,7 @@ export default config({
lists: createSchema({
ListName: list({
fields: {
repo: file({
isRequired: true,
}),
repo: file(),
/* ... */
},
}),
Expand All @@ -651,10 +645,6 @@ An `image` field represents an image file, i.e. `.jpg`, `.png`, `.webp`, or `.gi

See [`config.images`](./config#images) for details on how to configure your Keystone system with support for the `image` field type.

Options:

- `isRequired` (default: `false`): If `true` then this field can never be set to `null`.

```typescript
import { config, createSchema, list } from '@keystone-next/keystone';
import { image } from '@keystone-next/keystone/fields';
Expand All @@ -663,9 +653,7 @@ export default config({
lists: createSchema({
ListName: list({
fields: {
avatar: image({
isRequired: true,
}),
avatar: image(),
/* ... */
},
}),
Expand Down Expand Up @@ -722,7 +710,6 @@ export default config({

(coming soon)

- `isRequired` (default: `false`): If `true` then this field can never be set to `null`.
- `cloudinary`: Configuration for the connected Cloudinary account.
- `cloudName`
- `apiKey`
Expand All @@ -738,7 +725,6 @@ export default config({
ListName: list({
fields: {
fieldName: cloudinaryImage({
isRequired: true,
cloudinary: {
cloudName: process.env.CLOUDINARY_CLOUD_NAME,
apiKey: process.env.CLOUDINARY_API_KEY,
Expand Down
6 changes: 0 additions & 6 deletions packages/cloudinary/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
FieldTypeFunc,
jsonFieldTypePolyfilledForSQLite,
graphql,
FieldDefaultValue,
} from '@keystone-next/keystone/types';
import { FileUpload } from 'graphql-upload';
import cuid from 'cuid';
Expand All @@ -23,8 +22,6 @@ type StoredFile = {

type CloudinaryImageFieldConfig<TGeneratedListTypes extends BaseGeneratedListTypes> =
CommonFieldConfig<TGeneratedListTypes> & {
isRequired?: boolean;
defaultValue?: FieldDefaultValue<any, TGeneratedListTypes>;
cloudinary: {
cloudName: string;
apiKey: string;
Expand Down Expand Up @@ -111,8 +108,6 @@ const outputType = graphql.object<CloudinaryImage_File>()({
export const cloudinaryImage =
<TGeneratedListTypes extends BaseGeneratedListTypes>({
cloudinary,
isRequired,
defaultValue,
...config
}: CloudinaryImageFieldConfig<TGeneratedListTypes>): FieldTypeFunc =>
meta => {
Expand Down Expand Up @@ -170,6 +165,5 @@ export const cloudinaryImage =
},
}),
views: path.join(path.dirname(__dirname), 'views'),
__legacy: { isRequired, defaultValue },
});
};
1 change: 1 addition & 0 deletions packages/cloudinary/src/test-fixtures.skip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const prepareFile = (_filePath: string) => {
export const name = 'CloudinaryImage';
export const typeFunction = cloudinaryImage;
export const supportsUnique = false;
export const skipRequiredTest = true;
export const fieldName = 'image';
export const subfieldName = 'originalFilename';

Expand Down
18 changes: 4 additions & 14 deletions packages/keystone/src/fields/types/file/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,12 @@ import {
BaseGeneratedListTypes,
KeystoneContext,
FileData,
FieldDefaultValue,
} from '../../../types';
import { resolveView } from '../../resolve-view';
import { getFileRef } from './utils';

export type FileFieldConfig<TGeneratedListTypes extends BaseGeneratedListTypes> =
CommonFieldConfig<TGeneratedListTypes> & {
isRequired?: boolean;
defaultValue?: FieldDefaultValue<FileFieldInputType, TGeneratedListTypes>;
};
CommonFieldConfig<TGeneratedListTypes>;

const FileFieldInput = graphql.inputObject({
name: 'FileFieldInput',
Expand Down Expand Up @@ -84,11 +80,9 @@ async function inputResolver(data: FileFieldInputType, context: KeystoneContext)
}

export const file =
<TGeneratedListTypes extends BaseGeneratedListTypes>({
isRequired,
defaultValue,
...config
}: FileFieldConfig<TGeneratedListTypes> = {}): FieldTypeFunc =>
<TGeneratedListTypes extends BaseGeneratedListTypes>(
config: FileFieldConfig<TGeneratedListTypes> = {}
): FieldTypeFunc =>
() => {
if ((config as any).isIndexed === 'unique') {
throw Error("isIndexed: 'unique' is not a supported option for field type file");
Expand Down Expand Up @@ -123,9 +117,5 @@ export const file =
}),
unreferencedConcreteInterfaceImplementations: [LocalFileFieldOutput],
views: resolveView('file/views'),
__legacy: {
isRequired,
defaultValue,
},
});
};
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export const createReturnedValue = 3250;
export const updateReturnedValue = 5562;

export const supportsUnique = false;
export const skipRequiredTest = true;
export const fieldName = 'secretFile';
export const subfieldName = 'filesize';

Expand Down
18 changes: 4 additions & 14 deletions packages/keystone/src/fields/types/image/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { FileUpload } from 'graphql-upload';
import {
BaseGeneratedListTypes,
FieldDefaultValue,
fieldType,
FieldTypeFunc,
CommonFieldConfig,
Expand All @@ -14,10 +13,7 @@ import { resolveView } from '../../resolve-view';
import { getImageRef, SUPPORTED_IMAGE_EXTENSIONS } from './utils';

export type ImageFieldConfig<TGeneratedListTypes extends BaseGeneratedListTypes> =
CommonFieldConfig<TGeneratedListTypes> & {
defaultValue?: FieldDefaultValue<ImageFieldInputType, TGeneratedListTypes>;
isRequired?: boolean;
};
CommonFieldConfig<TGeneratedListTypes>;

const ImageExtensionEnum = graphql.enum({
name: 'ImageExtension',
Expand Down Expand Up @@ -96,11 +92,9 @@ function isValidImageExtension(extension: string): extension is ImageExtension {
}

export const image =
<TGeneratedListTypes extends BaseGeneratedListTypes>({
isRequired,
defaultValue,
...config
}: ImageFieldConfig<TGeneratedListTypes> = {}): FieldTypeFunc =>
<TGeneratedListTypes extends BaseGeneratedListTypes>(
config: ImageFieldConfig<TGeneratedListTypes> = {}
): FieldTypeFunc =>
() => {
if ((config as any).isIndexed === 'unique') {
throw Error("isIndexed: 'unique' is not a supported option for field type image");
Expand Down Expand Up @@ -142,9 +136,5 @@ export const image =
}),
unreferencedConcreteInterfaceImplementations: [LocalImageFieldOutput],
views: resolveView('image/views'),
__legacy: {
isRequired,
defaultValue,
},
});
};
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export const createReturnedValue = 'jpg';
export const updateReturnedValue = createReturnedValue;

export const supportsUnique = false;
export const skipRequiredTest = true;
export const fieldName = 'avatar';
export const subfieldName = 'extension';

Expand Down

0 comments on commit 4048991

Please sign in to comment.