fix: filesRequiredOnCreate typing, tests, linting (#3737)
This commit is contained in:
@@ -162,8 +162,8 @@ const collectionSchema = joi.object().keys({
|
|||||||
adminThumbnail: joi.alternatives().try(joi.string(), joi.func()),
|
adminThumbnail: joi.alternatives().try(joi.string(), joi.func()),
|
||||||
crop: joi.bool(),
|
crop: joi.bool(),
|
||||||
disableLocalStorage: joi.bool(),
|
disableLocalStorage: joi.bool(),
|
||||||
focalPoint: joi.bool(),
|
|
||||||
filesRequiredOnCreate: joi.bool(),
|
filesRequiredOnCreate: joi.bool(),
|
||||||
|
focalPoint: joi.bool(),
|
||||||
formatOptions: joi.object().keys({
|
formatOptions: joi.object().keys({
|
||||||
format: joi.string(),
|
format: joi.string(),
|
||||||
options: joi.object(),
|
options: joi.object(),
|
||||||
|
|||||||
@@ -63,6 +63,7 @@ export type IncomingUploadType = {
|
|||||||
adminThumbnail?: GetAdminThumbnail | string
|
adminThumbnail?: GetAdminThumbnail | string
|
||||||
crop?: boolean
|
crop?: boolean
|
||||||
disableLocalStorage?: boolean
|
disableLocalStorage?: boolean
|
||||||
|
filesRequiredOnCreate?: boolean
|
||||||
focalPoint?: boolean
|
focalPoint?: boolean
|
||||||
/** Options for original upload file only. For sizes, set each formatOptions individually. */
|
/** Options for original upload file only. For sizes, set each formatOptions individually. */
|
||||||
formatOptions?: ImageUploadFormatOptions
|
formatOptions?: ImageUploadFormatOptions
|
||||||
@@ -80,6 +81,7 @@ export type Upload = {
|
|||||||
adminThumbnail?: GetAdminThumbnail | string
|
adminThumbnail?: GetAdminThumbnail | string
|
||||||
crop?: boolean
|
crop?: boolean
|
||||||
disableLocalStorage?: boolean
|
disableLocalStorage?: boolean
|
||||||
|
filesRequiredOnCreate?: boolean
|
||||||
focalPoint?: boolean
|
focalPoint?: boolean
|
||||||
formatOptions?: ImageUploadFormatOptions
|
formatOptions?: ImageUploadFormatOptions
|
||||||
handlers?: any[]
|
handlers?: any[]
|
||||||
@@ -90,7 +92,6 @@ export type Upload = {
|
|||||||
staticOptions?: serveStatic.ServeStaticOptions<express.Response<any, Record<string, any>>>
|
staticOptions?: serveStatic.ServeStaticOptions<express.Response<any, Record<string, any>>>
|
||||||
staticURL: string
|
staticURL: string
|
||||||
trimOptions?: ImageUploadTrimOptions
|
trimOptions?: ImageUploadTrimOptions
|
||||||
filesRequiredOnCreate?: boolean
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export type File = {
|
export type File = {
|
||||||
|
|||||||
@@ -401,6 +401,24 @@ export default buildConfigWithDefaults({
|
|||||||
Uploads1,
|
Uploads1,
|
||||||
Uploads2,
|
Uploads2,
|
||||||
AdminThumbnailCol,
|
AdminThumbnailCol,
|
||||||
|
{
|
||||||
|
slug: 'optional-file',
|
||||||
|
upload: {
|
||||||
|
staticURL: '/optional',
|
||||||
|
staticDir: './optional',
|
||||||
|
filesRequiredOnCreate: false,
|
||||||
|
},
|
||||||
|
fields: [],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
slug: 'required-file',
|
||||||
|
upload: {
|
||||||
|
staticURL: '/required',
|
||||||
|
staticDir: './required',
|
||||||
|
filesRequiredOnCreate: true,
|
||||||
|
},
|
||||||
|
fields: [],
|
||||||
|
},
|
||||||
],
|
],
|
||||||
onInit: async (payload) => {
|
onInit: async (payload) => {
|
||||||
const uploadsDir = path.resolve(__dirname, './media')
|
const uploadsDir = path.resolve(__dirname, './media')
|
||||||
|
|||||||
@@ -540,6 +540,48 @@ describe('Collections - Uploads', () => {
|
|||||||
|
|
||||||
expect(await fileExists(path.join(__dirname, doc.filename))).toBe(false)
|
expect(await fileExists(path.join(__dirname, doc.filename))).toBe(false)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe('filesRequiredOnCreate', () => {
|
||||||
|
// eslint-disable-next-line @typescript-eslint/require-await
|
||||||
|
it('should allow file to be optional if filesRequiredOnCreate is false', async () => {
|
||||||
|
expect(
|
||||||
|
async () =>
|
||||||
|
await payload.create({
|
||||||
|
// @ts-ignore
|
||||||
|
collection: 'optional-file',
|
||||||
|
data: {},
|
||||||
|
}),
|
||||||
|
).not.toThrow()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should throw an error if no file and filesRequiredOnCreate is true', async () => {
|
||||||
|
await expect(async () =>
|
||||||
|
payload.create({
|
||||||
|
// @ts-ignore
|
||||||
|
collection: 'required-file',
|
||||||
|
data: {},
|
||||||
|
}),
|
||||||
|
).rejects.toThrow(
|
||||||
|
expect.objectContaining({
|
||||||
|
name: 'MissingFile',
|
||||||
|
message: 'No files were uploaded.',
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
})
|
||||||
|
it('should throw an error if no file and filesRequiredOnCreate is not defined', async () => {
|
||||||
|
await expect(async () =>
|
||||||
|
payload.create({
|
||||||
|
collection: mediaSlug,
|
||||||
|
data: {},
|
||||||
|
}),
|
||||||
|
).rejects.toThrow(
|
||||||
|
expect.objectContaining({
|
||||||
|
name: 'MissingFile',
|
||||||
|
message: 'No files were uploaded.',
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
async function fileExists(fileName: string): Promise<boolean> {
|
async function fileExists(fileName: string): Promise<boolean> {
|
||||||
|
|||||||
@@ -11,13 +11,23 @@ export interface Config {
|
|||||||
relation: Relation
|
relation: Relation
|
||||||
audio: Audio
|
audio: Audio
|
||||||
'gif-resize': GifResize
|
'gif-resize': GifResize
|
||||||
|
'no-image-sizes': NoImageSize
|
||||||
|
'crop-only': CropOnly
|
||||||
|
'focal-only': FocalOnly
|
||||||
media: Media
|
media: Media
|
||||||
|
enlarge: Enlarge
|
||||||
|
reduce: Reduce
|
||||||
'media-trim': MediaTrim
|
'media-trim': MediaTrim
|
||||||
'unstored-media': UnstoredMedia
|
'unstored-media': UnstoredMedia
|
||||||
'externally-served-media': ExternallyServedMedia
|
'externally-served-media': ExternallyServedMedia
|
||||||
'uploads-1': Uploads1
|
'uploads-1': Uploads1
|
||||||
'uploads-2': Uploads2
|
'uploads-2': Uploads2
|
||||||
|
'admin-thumbnail': AdminThumbnail
|
||||||
|
'optional-file': OptionalFile
|
||||||
|
'required-file': RequiredFile
|
||||||
users: User
|
users: User
|
||||||
|
'payload-preferences': PayloadPreference
|
||||||
|
'payload-migrations': PayloadMigration
|
||||||
}
|
}
|
||||||
globals: {}
|
globals: {}
|
||||||
}
|
}
|
||||||
@@ -70,6 +80,14 @@ export interface Media {
|
|||||||
filesize?: number
|
filesize?: number
|
||||||
filename?: string
|
filename?: string
|
||||||
}
|
}
|
||||||
|
accidentalSameSize?: {
|
||||||
|
url?: string
|
||||||
|
width?: number
|
||||||
|
height?: number
|
||||||
|
mimeType?: string
|
||||||
|
filesize?: number
|
||||||
|
filename?: string
|
||||||
|
}
|
||||||
tablet?: {
|
tablet?: {
|
||||||
url?: string
|
url?: string
|
||||||
width?: number
|
width?: number
|
||||||
@@ -94,6 +112,62 @@ export interface Media {
|
|||||||
filesize?: number
|
filesize?: number
|
||||||
filename?: string
|
filename?: string
|
||||||
}
|
}
|
||||||
|
focalTest?: {
|
||||||
|
url?: string
|
||||||
|
width?: number
|
||||||
|
height?: number
|
||||||
|
mimeType?: string
|
||||||
|
filesize?: number
|
||||||
|
filename?: string
|
||||||
|
}
|
||||||
|
focalTest2?: {
|
||||||
|
url?: string
|
||||||
|
width?: number
|
||||||
|
height?: number
|
||||||
|
mimeType?: string
|
||||||
|
filesize?: number
|
||||||
|
filename?: string
|
||||||
|
}
|
||||||
|
focalTest3?: {
|
||||||
|
url?: string
|
||||||
|
width?: number
|
||||||
|
height?: number
|
||||||
|
mimeType?: string
|
||||||
|
filesize?: number
|
||||||
|
filename?: string
|
||||||
|
}
|
||||||
|
focalTest4?: {
|
||||||
|
url?: string
|
||||||
|
width?: number
|
||||||
|
height?: number
|
||||||
|
mimeType?: string
|
||||||
|
filesize?: number
|
||||||
|
filename?: string
|
||||||
|
}
|
||||||
|
focalTest5?: {
|
||||||
|
url?: string
|
||||||
|
width?: number
|
||||||
|
height?: number
|
||||||
|
mimeType?: string
|
||||||
|
filesize?: number
|
||||||
|
filename?: string
|
||||||
|
}
|
||||||
|
focalTest6?: {
|
||||||
|
url?: string
|
||||||
|
width?: number
|
||||||
|
height?: number
|
||||||
|
mimeType?: string
|
||||||
|
filesize?: number
|
||||||
|
filename?: string
|
||||||
|
}
|
||||||
|
focalTest7?: {
|
||||||
|
url?: string
|
||||||
|
width?: number
|
||||||
|
height?: number
|
||||||
|
mimeType?: string
|
||||||
|
filesize?: number
|
||||||
|
filename?: string
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
export interface Audio {
|
export interface Audio {
|
||||||
@@ -131,6 +205,189 @@ export interface GifResize {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
export interface NoImageSize {
|
||||||
|
id: string
|
||||||
|
updatedAt: string
|
||||||
|
createdAt: string
|
||||||
|
url?: string
|
||||||
|
filename?: string
|
||||||
|
mimeType?: string
|
||||||
|
filesize?: number
|
||||||
|
width?: number
|
||||||
|
height?: number
|
||||||
|
}
|
||||||
|
export interface CropOnly {
|
||||||
|
id: string
|
||||||
|
updatedAt: string
|
||||||
|
createdAt: string
|
||||||
|
url?: string
|
||||||
|
filename?: string
|
||||||
|
mimeType?: string
|
||||||
|
filesize?: number
|
||||||
|
width?: number
|
||||||
|
height?: number
|
||||||
|
sizes?: {
|
||||||
|
focalTest?: {
|
||||||
|
url?: string
|
||||||
|
width?: number
|
||||||
|
height?: number
|
||||||
|
mimeType?: string
|
||||||
|
filesize?: number
|
||||||
|
filename?: string
|
||||||
|
}
|
||||||
|
focalTest2?: {
|
||||||
|
url?: string
|
||||||
|
width?: number
|
||||||
|
height?: number
|
||||||
|
mimeType?: string
|
||||||
|
filesize?: number
|
||||||
|
filename?: string
|
||||||
|
}
|
||||||
|
focalTest3?: {
|
||||||
|
url?: string
|
||||||
|
width?: number
|
||||||
|
height?: number
|
||||||
|
mimeType?: string
|
||||||
|
filesize?: number
|
||||||
|
filename?: string
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
export interface FocalOnly {
|
||||||
|
id: string
|
||||||
|
updatedAt: string
|
||||||
|
createdAt: string
|
||||||
|
url?: string
|
||||||
|
filename?: string
|
||||||
|
mimeType?: string
|
||||||
|
filesize?: number
|
||||||
|
width?: number
|
||||||
|
height?: number
|
||||||
|
sizes?: {
|
||||||
|
focalTest?: {
|
||||||
|
url?: string
|
||||||
|
width?: number
|
||||||
|
height?: number
|
||||||
|
mimeType?: string
|
||||||
|
filesize?: number
|
||||||
|
filename?: string
|
||||||
|
}
|
||||||
|
focalTest2?: {
|
||||||
|
url?: string
|
||||||
|
width?: number
|
||||||
|
height?: number
|
||||||
|
mimeType?: string
|
||||||
|
filesize?: number
|
||||||
|
filename?: string
|
||||||
|
}
|
||||||
|
focalTest3?: {
|
||||||
|
url?: string
|
||||||
|
width?: number
|
||||||
|
height?: number
|
||||||
|
mimeType?: string
|
||||||
|
filesize?: number
|
||||||
|
filename?: string
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
export interface Enlarge {
|
||||||
|
id: string
|
||||||
|
updatedAt: string
|
||||||
|
createdAt: string
|
||||||
|
url?: string
|
||||||
|
filename?: string
|
||||||
|
mimeType?: string
|
||||||
|
filesize?: number
|
||||||
|
width?: number
|
||||||
|
height?: number
|
||||||
|
sizes?: {
|
||||||
|
accidentalSameSize?: {
|
||||||
|
url?: string
|
||||||
|
width?: number
|
||||||
|
height?: number
|
||||||
|
mimeType?: string
|
||||||
|
filesize?: number
|
||||||
|
filename?: string
|
||||||
|
}
|
||||||
|
sameSizeWithNewFormat?: {
|
||||||
|
url?: string
|
||||||
|
width?: number
|
||||||
|
height?: number
|
||||||
|
mimeType?: string
|
||||||
|
filesize?: number
|
||||||
|
filename?: string
|
||||||
|
}
|
||||||
|
resizedLarger?: {
|
||||||
|
url?: string
|
||||||
|
width?: number
|
||||||
|
height?: number
|
||||||
|
mimeType?: string
|
||||||
|
filesize?: number
|
||||||
|
filename?: string
|
||||||
|
}
|
||||||
|
resizedSmaller?: {
|
||||||
|
url?: string
|
||||||
|
width?: number
|
||||||
|
height?: number
|
||||||
|
mimeType?: string
|
||||||
|
filesize?: number
|
||||||
|
filename?: string
|
||||||
|
}
|
||||||
|
widthLowerHeightLarger?: {
|
||||||
|
url?: string
|
||||||
|
width?: number
|
||||||
|
height?: number
|
||||||
|
mimeType?: string
|
||||||
|
filesize?: number
|
||||||
|
filename?: string
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
export interface Reduce {
|
||||||
|
id: string
|
||||||
|
updatedAt: string
|
||||||
|
createdAt: string
|
||||||
|
url?: string
|
||||||
|
filename?: string
|
||||||
|
mimeType?: string
|
||||||
|
filesize?: number
|
||||||
|
width?: number
|
||||||
|
height?: number
|
||||||
|
sizes?: {
|
||||||
|
accidentalSameSize?: {
|
||||||
|
url?: string
|
||||||
|
width?: number
|
||||||
|
height?: number
|
||||||
|
mimeType?: string
|
||||||
|
filesize?: number
|
||||||
|
filename?: string
|
||||||
|
}
|
||||||
|
sameSizeWithNewFormat?: {
|
||||||
|
url?: string
|
||||||
|
width?: number
|
||||||
|
height?: number
|
||||||
|
mimeType?: string
|
||||||
|
filesize?: number
|
||||||
|
filename?: string
|
||||||
|
}
|
||||||
|
resizedLarger?: {
|
||||||
|
url?: string
|
||||||
|
width?: number
|
||||||
|
height?: number
|
||||||
|
mimeType?: string
|
||||||
|
filesize?: number
|
||||||
|
filename?: string
|
||||||
|
}
|
||||||
|
resizedSmaller?: {
|
||||||
|
url?: string
|
||||||
|
width?: number
|
||||||
|
height?: number
|
||||||
|
mimeType?: string
|
||||||
|
filesize?: number
|
||||||
|
filename?: string
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
export interface MediaTrim {
|
export interface MediaTrim {
|
||||||
id: string
|
id: string
|
||||||
updatedAt: string
|
updatedAt: string
|
||||||
@@ -217,6 +474,39 @@ export interface Uploads2 {
|
|||||||
width?: number
|
width?: number
|
||||||
height?: number
|
height?: number
|
||||||
}
|
}
|
||||||
|
export interface AdminThumbnail {
|
||||||
|
id: string
|
||||||
|
updatedAt: string
|
||||||
|
createdAt: string
|
||||||
|
url?: string
|
||||||
|
filename?: string
|
||||||
|
mimeType?: string
|
||||||
|
filesize?: number
|
||||||
|
width?: number
|
||||||
|
height?: number
|
||||||
|
}
|
||||||
|
export interface OptionalFile {
|
||||||
|
id: string
|
||||||
|
updatedAt: string
|
||||||
|
createdAt: string
|
||||||
|
url?: string
|
||||||
|
filename?: string
|
||||||
|
mimeType?: string
|
||||||
|
filesize?: number
|
||||||
|
width?: number
|
||||||
|
height?: number
|
||||||
|
}
|
||||||
|
export interface RequiredFile {
|
||||||
|
id: string
|
||||||
|
updatedAt: string
|
||||||
|
createdAt: string
|
||||||
|
url?: string
|
||||||
|
filename?: string
|
||||||
|
mimeType?: string
|
||||||
|
filesize?: number
|
||||||
|
width?: number
|
||||||
|
height?: number
|
||||||
|
}
|
||||||
export interface User {
|
export interface User {
|
||||||
id: string
|
id: string
|
||||||
updatedAt: string
|
updatedAt: string
|
||||||
@@ -228,5 +518,58 @@ export interface User {
|
|||||||
hash?: string
|
hash?: string
|
||||||
loginAttempts?: number
|
loginAttempts?: number
|
||||||
lockUntil?: string
|
lockUntil?: string
|
||||||
password?: string
|
password: string
|
||||||
|
}
|
||||||
|
export interface PayloadPreference {
|
||||||
|
id: string
|
||||||
|
user: {
|
||||||
|
relationTo: 'users'
|
||||||
|
value: string | User
|
||||||
|
}
|
||||||
|
key?: string
|
||||||
|
value?:
|
||||||
|
| {
|
||||||
|
[k: string]: unknown
|
||||||
|
}
|
||||||
|
| unknown[]
|
||||||
|
| string
|
||||||
|
| number
|
||||||
|
| boolean
|
||||||
|
| null
|
||||||
|
updatedAt: string
|
||||||
|
createdAt: string
|
||||||
|
}
|
||||||
|
export interface PayloadMigration {
|
||||||
|
id: string
|
||||||
|
name?: string
|
||||||
|
batch?: number
|
||||||
|
updatedAt: string
|
||||||
|
createdAt: string
|
||||||
|
}
|
||||||
|
|
||||||
|
declare module 'payload' {
|
||||||
|
export interface GeneratedTypes {
|
||||||
|
collections: {
|
||||||
|
relation: Relation
|
||||||
|
audio: Audio
|
||||||
|
'gif-resize': GifResize
|
||||||
|
'no-image-sizes': NoImageSize
|
||||||
|
'crop-only': CropOnly
|
||||||
|
'focal-only': FocalOnly
|
||||||
|
media: Media
|
||||||
|
enlarge: Enlarge
|
||||||
|
reduce: Reduce
|
||||||
|
'media-trim': MediaTrim
|
||||||
|
'unstored-media': UnstoredMedia
|
||||||
|
'externally-served-media': ExternallyServedMedia
|
||||||
|
'uploads-1': Uploads1
|
||||||
|
'uploads-2': Uploads2
|
||||||
|
'admin-thumbnail': AdminThumbnail
|
||||||
|
'optional-file': OptionalFile
|
||||||
|
'required-file': RequiredFile
|
||||||
|
users: User
|
||||||
|
'payload-preferences': PayloadPreference
|
||||||
|
'payload-migrations': PayloadMigration
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user