Skip to content

Commit 726a8b6

Browse files
fix: Validation for file like objects (#246)
* Fix validation for file like objects * Update npm tag for official releases to latest
1 parent d21c4f6 commit 726a8b6

4 files changed

Lines changed: 17 additions & 11 deletions

File tree

scripts/release.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ async function getNewVersion() {
107107
const tags = await getVersionsFromGitTags();
108108
const latestOfficialTag = tags.find(tag => !tag.includes('-beta.') && !tag.includes('-dev.'));
109109
// If no official version found, use v0.0.0 as the starting point
110-
const latestOfficialVersion = latestOfficialTag
110+
const latestOfficialVersion = latestOfficialTag
111111
? semver.coerce(latestOfficialTag).version
112112
: '0.0.0';
113113

@@ -150,7 +150,7 @@ async function gitCommit({ newVersion, releaseType }) {
150150

151151
async function publish({ newVersion, releaseType, otp }) {
152152
console.log('Publishing new version...');
153-
const npmTag = `v1-${releaseType}`;
153+
const npmTag = releaseType === 'official' ? 'latest' : `v1-${releaseType}`;
154154
const originalVersion = packageJson.version;
155155

156156
try {
@@ -168,7 +168,7 @@ async function publish({ newVersion, releaseType, otp }) {
168168
if (releaseType === 'beta') {
169169
console.log(`✍️ REMINDER: Please publish the release on Github too as "pre-release".`);
170170
}
171-
171+
172172
if (releaseType === 'official') {
173173
console.log(`✍️ REMINDER: Please publish the release on Github too.`);
174174
}

src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export type JsfSchemaType = Exclude<JSONSchema, boolean>['type']
99
/**
1010
* Defines the type of a value in the form that will be validated against the schema.
1111
*/
12-
export type SchemaValue = string | number | ObjectValue | null | undefined | Array<SchemaValue> | boolean
12+
export type SchemaValue = string | number | ObjectValue | null | undefined | Array<SchemaValue> | boolean | File
1313

1414
/**
1515
* A nested object value.

src/validation/file.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import type { NonBooleanJsfSchema, SchemaValue } from '../types'
33
import { isObjectValue } from './util'
44

55
// Represents a file-like object, either a browser native File or a plain object.
6-
// Both must have name (string) and size (number) properties.
7-
export type FileLike = (File & { name: string, size: number }) | { name: string, size: number }
6+
// A plain object must have a name (string) property.
7+
export type FileLike = File | { name: string, size?: number }
88

99
/**
1010
* Validates file-specific constraints (maxFileSize, accept).
@@ -48,7 +48,7 @@ export function validateFile(
4848

4949
// 2. Check structure of array items: Each item must be a FileLike object.
5050
const isStructureValid = value.every(
51-
file => isObjectValue(file) && typeof file.name === 'string' && typeof file.size === 'number',
51+
file => isObjectValue(file) && (typeof file.name === 'string' || file instanceof File),
5252
)
5353

5454
if (!isStructureValid) {
@@ -62,7 +62,7 @@ export function validateFile(
6262
if (typeof presentation?.maxFileSize === 'number') {
6363
const maxSizeInBytes = presentation.maxFileSize * 1024 // Convert KB from schema to Bytes
6464
// Check if *any* file exceeds the limit.
65-
const isAnyFileTooLarge = files.some(file => file.size > maxSizeInBytes)
65+
const isAnyFileTooLarge = files.some(file => (file.size ?? 0) > maxSizeInBytes)
6666

6767
if (isAnyFileTooLarge) {
6868
return [{ path, validation: 'maxFileSize', schema, value }]

test/validation/file.test.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,16 @@ describe('validateFile', () => {
8888
expect(errors).toEqual([errorLike({ path: [], validation: 'type' })])
8989
})
9090

91-
it('should fail validation for array with invalid file object (missing size)', () => {
92-
const value = [{ name: 'file.txt' }] as any[] // Cast to bypass TS check, simulating bad input
91+
it('should pass validation for array with file objects with only name', () => {
92+
const value = [{ name: 'file.txt' }]
9393
const errors = validateSchema(value, fileSchemaWithSizeLimitKB)
94-
expect(errors).toEqual([errorLike({ path: [], validation: 'fileStructure' })])
94+
expect(errors).toEqual([])
95+
})
96+
97+
it('should pass validation for array with File instances', () => {
98+
const value = [new File(['file contents'], 'file.txt', { type: 'text/plain' })]
99+
const errors = validateSchema(value, fileSchemaWithSizeLimitKB)
100+
expect(errors).toEqual([])
95101
})
96102

97103
it('should fail validation for array with invalid file object (missing name)', () => {

0 commit comments

Comments
 (0)