diff --git a/scripts/release.js b/scripts/release.js index f413163a..a32bd0f1 100644 --- a/scripts/release.js +++ b/scripts/release.js @@ -107,7 +107,7 @@ async function getNewVersion() { const tags = await getVersionsFromGitTags(); const latestOfficialTag = tags.find(tag => !tag.includes('-beta.') && !tag.includes('-dev.')); // If no official version found, use v0.0.0 as the starting point - const latestOfficialVersion = latestOfficialTag + const latestOfficialVersion = latestOfficialTag ? semver.coerce(latestOfficialTag).version : '0.0.0'; @@ -150,7 +150,7 @@ async function gitCommit({ newVersion, releaseType }) { async function publish({ newVersion, releaseType, otp }) { console.log('Publishing new version...'); - const npmTag = `v1-${releaseType}`; + const npmTag = releaseType === 'official' ? 'latest' : `v1-${releaseType}`; const originalVersion = packageJson.version; try { @@ -168,7 +168,7 @@ async function publish({ newVersion, releaseType, otp }) { if (releaseType === 'beta') { console.log(`✍️ REMINDER: Please publish the release on Github too as "pre-release".`); } - + if (releaseType === 'official') { console.log(`✍️ REMINDER: Please publish the release on Github too.`); } diff --git a/src/types.ts b/src/types.ts index 817f9a54..c08c7ab0 100644 --- a/src/types.ts +++ b/src/types.ts @@ -9,7 +9,7 @@ export type JsfSchemaType = Exclude['type'] /** * Defines the type of a value in the form that will be validated against the schema. */ -export type SchemaValue = string | number | ObjectValue | null | undefined | Array | boolean +export type SchemaValue = string | number | ObjectValue | null | undefined | Array | boolean | File /** * A nested object value. diff --git a/src/validation/file.ts b/src/validation/file.ts index f3aff0a2..3affbcc3 100644 --- a/src/validation/file.ts +++ b/src/validation/file.ts @@ -3,8 +3,8 @@ import type { NonBooleanJsfSchema, SchemaValue } from '../types' import { isObjectValue } from './util' // Represents a file-like object, either a browser native File or a plain object. -// Both must have name (string) and size (number) properties. -export type FileLike = (File & { name: string, size: number }) | { name: string, size: number } +// A plain object must have a name (string) property. +export type FileLike = File | { name: string, size?: number } /** * Validates file-specific constraints (maxFileSize, accept). @@ -48,7 +48,7 @@ export function validateFile( // 2. Check structure of array items: Each item must be a FileLike object. const isStructureValid = value.every( - file => isObjectValue(file) && typeof file.name === 'string' && typeof file.size === 'number', + file => isObjectValue(file) && (typeof file.name === 'string' || file instanceof File), ) if (!isStructureValid) { @@ -62,7 +62,7 @@ export function validateFile( if (typeof presentation?.maxFileSize === 'number') { const maxSizeInBytes = presentation.maxFileSize * 1024 // Convert KB from schema to Bytes // Check if *any* file exceeds the limit. - const isAnyFileTooLarge = files.some(file => file.size > maxSizeInBytes) + const isAnyFileTooLarge = files.some(file => (file.size ?? 0) > maxSizeInBytes) if (isAnyFileTooLarge) { return [{ path, validation: 'maxFileSize', schema, value }] diff --git a/test/validation/file.test.ts b/test/validation/file.test.ts index 0d531263..f2aaebfa 100644 --- a/test/validation/file.test.ts +++ b/test/validation/file.test.ts @@ -88,10 +88,16 @@ describe('validateFile', () => { expect(errors).toEqual([errorLike({ path: [], validation: 'type' })]) }) - it('should fail validation for array with invalid file object (missing size)', () => { - const value = [{ name: 'file.txt' }] as any[] // Cast to bypass TS check, simulating bad input + it('should pass validation for array with file objects with only name', () => { + const value = [{ name: 'file.txt' }] const errors = validateSchema(value, fileSchemaWithSizeLimitKB) - expect(errors).toEqual([errorLike({ path: [], validation: 'fileStructure' })]) + expect(errors).toEqual([]) + }) + + it('should pass validation for array with File instances', () => { + const value = [new File(['file contents'], 'file.txt', { type: 'text/plain' })] + const errors = validateSchema(value, fileSchemaWithSizeLimitKB) + expect(errors).toEqual([]) }) it('should fail validation for array with invalid file object (missing name)', () => {