npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

express-fileupload-validator

v2.0.1

Published

Validator for express-fileupload package

Downloads

39

Readme

express-fileupload-validator

NPM GitHub package.json version CircleCI

Validator for express-fileupload package.

Installation

npm i express-fileupload

Prerequisites

In order to use this package you need to use express alongside express-fileupload.

const express = require('express');
const expressFileupload = require('express-fileupload');

const app = express();
// ...
app.use(expressFileupload());

app.listen(8080);

Basic Usage

const express = require('express');
const { ExpressFileuploadValidator } = require('express-fileupload-validator');

// You can export it to reuse the same validations.
const expressFileuploadValidator = new ExpressFileuploadValidator({
  minCount: 2,
  maxCount: 8,
  allowedExtensions: ['jpg', 'png', 'gif'],
  allowedMimetypes: ['image/jpg', 'image/jpeg', 'image/png', 'image/gif'],
  maxSize: '20MB',
});

const router = Router();

router.post('/upload-images', (req, res) => {
  if (!req.files || !req.files.images) {
    // We didn't get any files at all here..
  }

  try {
    expressFileuploadValidator.validate(req.files.images); // Validate the file or files.
    // Everything ok we can save the files safely now :)
  } catch (e) {
    console.log(e.errors); // Validation error messages...
  }
});

API

new ExpressFileuploadValidator(options?, errorMessages?)

options

Type: object

In addition, you can specify the below options.

minCount

Type: number
Default: -1 (no limit)

The number of minimum files.

maxCount

Type: number
Default: -1 (no limit)

The number of maximum files.

minSize

Type: string | number
Default: -1 (no limit)

The minimum size per file.
Can be formatted size, for example: 2MB, 5GB and etc.
Can be size in bytes, for example: 6291456 which is 6MB.

maxSize

Type: string | number
Default: -1 (no limit)

The maximum size per file.
Can be formatted size, for example: 2MB, 5GB and etc.
Can be size in bytes, for example: 6291456 which is 6MB.

allowedExtensions

Type: string[]
Default: []

List of allowed file extensions, for example: ['jpg', 'png'].

disallowedExtensions

Type: string[]
Default: []

List of disallowed file extensions, for example: ['jpg', 'png'].

allowedMimetypes

Type: string[]
Default: []

List of allowed file mimetypes, for example: ['image/jpg', 'image/png'].

disallowedMimetypes

Type: string[]
Default: []

List of disallowed file mimetypes, for example: ['image/jpg', 'image/png'].

abortEarly

Type: bool
Default: true

Will decide if to abort as soon as possible, that means you will always gets the first error. If abortEarly is false you will get all collected files errors.

errorMessages

You can specify object with your custom error messages. Please notice you need to keep the placeholders {} to keep the error messages clean.

const expressFileuploadValidator = new ExpressFileuploadValidator({}, {
  minCount: 'Too few files, minimum {0} are expected but {1} are given',
  maxCount: 'Too many files, maximum {0} are allowed but {1} are given',
  minSize: 'Minimum expected size for file {0} is {1} but {2} detected',
  maxSize: 'Maximum allowed size for file {0} is {1} but {2} detected',
  allowedExtensions:
    'File {0} has an incorrect extension of {1}, allowed extensions are: {2}',
  disallowedExtensions:
    'File {0} has an incorrect extension of {1}, disallowed extensions are: {2}',
  allowedMimetypes:
    'File {0} has an incorrect mimetype of {1}, allowed mimetypes are: {2}',
  disallowedMimetypes:
    'File {0} has an incorrect mimetype of {1}, disallowed mimetypes are: {2}',
});

ExpressFileuploadValidator.setOptions(options)

ExpressFileuploadValidator.getOptions()

ExpressFileuploadValidator.setErrorMessages(errorMessages)

ExpressFileuploadValidator.getErrorMessages()

ExpressFileuploadValidator.validate()

Throws ExpressFileuploadValidatorErrors if there are errors.

License

MIT