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

nextjs-file-validator

v1.0.4

Published

A React hook for file validation

Downloads

30

Readme

nextjs-file-validator

nextjs-file-validator is a React hook for validating files. It supports validation for images and PDFs, including custom validation functions.

API

useFileValidation

Parameters

  • userOptions (optional): An object containing the validation options.

Options

  • showAlert (boolean): Show alert messages. Default is false.
  • sizeInKbAllowed (number): Maximum allowed file size in KB. Default is 10240 (10 MB).
  • allowedTypes (string[]): Array of allowed MIME types. Default is ['image/jpeg', 'image/png', 'application/pdf'].
  • heightOfImage (number): Maximum allowed height of an image. Default is 2000.
  • widthOfImage (number): Maximum allowed width of an image.
  • pdfPageMinCount (number): Minimum number of pages allowed in a PDF file. Default is 1.
  • pdfPageMaxCount (number): Maximum number of pages allowed in a PDF file. Default is 10.

Features

  • Validate file size, type, and dimensions.
  • Custom validation functions for images and PDFs.
  • Configurable alert messages.
  • Supports TypeScript.

Usage

import useFileValidation from 'nextjs-file-validator';

function MyComponent() {
    const { validateFile, error } = useFileValidation({
        sizeInKbAllowed: 5120, // 5 MB
        allowedTypes: ["image/jpeg", "image/png"],
        showAlert: true,
        pdfPageMinCount: 1,
        pdfPageMaxCount: 5,
        messages: {
            noFile: "Please select a file.",
            fileSize: "The file is too large!",
            fileType: "This file type is not supported!",
            dimensions: "The image dimensions are too large!",
        },
        customValidations: {
            image: [
                (file, image) => {
                    // Example custom validation: image must be a square
                    return image.width === image.height ? true : 'Image must be a square.';
                }
            ],
            pdf: [
                (file, pdfData, pdfText) => {
                    // Custom PDF validation
                    if (!pdfText.includes('Example Keyword')) {
                        return 'PDF does not contain the required keyword';
                    }
                    return true;
                }
            ]
        }
    });


    const handleFileChange = async (event) => {
        const file = event.target.files[0];
        try {
            await validateFile(file);
            console.log('File is valid');
            // Proceed with your logic after file validation
        } catch (error) {
            console.error('File validation failed:', error);
            // Handle error or display error message
        }
    };

    return (
        <div>
            <input type="file" onChange={handleFileChange} />
            {error && <div style={{ color: 'red' }}>{error}</div>}
        </div>
    );
}