Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions scripts/release.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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 {
Expand All @@ -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.`);
}
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export type JsfSchemaType = Exclude<JSONSchema, boolean>['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<SchemaValue> | boolean
export type SchemaValue = string | number | ObjectValue | null | undefined | Array<SchemaValue> | boolean | File

/**
* A nested object value.
Expand Down
8 changes: 4 additions & 4 deletions src/validation/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down Expand Up @@ -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) {
Expand All @@ -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 }]
Expand Down
12 changes: 9 additions & 3 deletions test/validation/file.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)', () => {
Expand Down