dotnet/aspnetcore · error
FileExtensions validator requires a non-empty "extensions" p
Error message
FileExtensions validator requires a non-empty "extensions" parameter.
What it means
Thrown by the FileExtensions validator (FileExtensions.ts:12) when params.extensions is falsy. This validator checks that an uploaded filename ends with one of the allowed extensions (e.g., '.png,.jpg'); without the extensions list there is nothing to validate, so it throws to surface the misconfiguration.
Source
Thrown at src/Components/Web.JS/src/Validation/Validators/FileExtensions.ts:12
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
import { ValidationContext, ValidationResult, Validator, pass, fail } from '../ValidationTypes';
// Validates that the filename ends with an allowed extension (case-insensitive).
// Extensions param is comma-separated with dot prefix (e.g. ".png,.jpg,.gif").
// Throws if the mandatory `extensions` parameter is missing.
export const fileExtensionsValidator: Validator = (context: ValidationContext): ValidationResult => {
const { value, params } = context;
if (!params.extensions) {
throw new Error('FileExtensions validator requires a non-empty "extensions" parameter.');
}
if (!value) {
return pass();
}
// Build regex from comma-separated extensions, stripping dots and escaping regex metacharacters.
const extensions = params.extensions.split(',')
.map(ext => ext.trim().replace(/^\./, ''))
.filter(ext => ext.length > 0)
.map(ext => ext.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'))
.join('|');
if (!extensions) {
return pass();
}
return new RegExp(`\\.(${extensions})$`, 'i').test(value) ? pass() : fail();View on GitHub (pinned to 294cab2f9b)
Solutions
- Add [FileExtensions(Extensions = ".png,.jpg,.gif")] to the IFormFile/string property and confirm the input renders data-val-fileextensions-extensions=".png,.jpg,.gif".
- When wiring manually, pass params: { extensions: '.png,.jpg' } (comma-separated, leading dots optional).
- Confirm no custom HTML helper is stripping data-val-* attributes from file inputs.
- Verify the attribute is on the property bound to the uploaded file, not a related display field.
Example fix
// before <input type="file" name="Upload" data-val-fileextensions /> // after <input type="file" name="Upload" data-val-fileextensions data-val-fileextensions-extensions=".png,.jpg" />
Defensive patterns
Strategy: validation
Validate before calling
function hasExtensionsParam(params: Record<string, unknown>): boolean {
return Boolean(params && typeof params.extensions === 'string' && params.extensions.length > 0);
} Type guard
function isFileExtensionsConfigured(p: any): p is { extensions: string } {
return p && typeof p.extensions === 'string' && p.extensions.trim().length > 0;
} Prevention
- Always supply Extensions=".png,.jpg" on [FileExtensions].
- Confirm data-val-fileextensions-extensions renders on the input.
- When wiring manually pass { extensions: '.png,.jpg' }.
- Guard against tag helpers stripping data-* attributes.
When it happens
Trigger: Invoking the fileExtensions validator with a params object missing 'extensions' or with extensions='' / null. Normally emitted from [FileExtensions(Extensions=".png,.jpg")]; if the attribute's data-val-fileextensions-extensions metadata is absent the JS validator cannot run.
Common situations: Model has [FileExtensions] but the attribute argument is empty or the tag helper does not emit data-val-fileextensions-extensions. Custom input rendering that drops data-* attributes. Manually registering the validator without supplying extensions.
Related errors
- EqualTo validator requires a non-empty "other" parameter.
- Range validator requires at least one of "min" or "max" para
- regex validator requires a non-empty "pattern" parameter.
- length/minlength/maxlength validator requires at least one o
- Unknown passkey operation '${this.attrs.operation}'.
AI-assisted analysis of dotnet/aspnetcore@294cab2f9b (2026-08-06).
Data as JSON: /api/errors/ab50cb4ecc602f87.
Report an issue: GitHub.