Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

github.com/dholtzmann/filevalidator

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/dholtzmann/filevalidator

  • v0.0.0-20171229015643-c539dd93d8ed
  • Source
  • Go
  • Socket score

Version published
Created
Source

filevalidator

Go simple package for validating file uploads.

Installation

go get -u github.com/dholtzmann/filevalidator

Basic Example

package main

import (
	fv "github.com/dholtzmann/filevalidator"
	"html/template"
	"net/http"
)

func main() {
	// setup HTTP server...
}

func indexPage(w http.ResponseWriter, r *http.Request) {
	tpl := template.Must(template.ParseGlob("./templates/*.html"))

	err := r.ParseMultipartForm(1 << 20)
	if err != nil {
		panic(err.Error())
	}

	fields := map[string]*Field{
		"imageupload":   NewField(true, false, FileSize(100, 100000), MimeTypes([]string{"image/png", "image/jpeg"}), MinPixelSize(100, 100), MaxPixelSize(500, 500)),
		"fileupload":    NewField(true, true, FileSize(100, 100000), MimeTypes([]string{"application/x-gzip"})),
		"missingupload": NewField(false, true, nil),
	}

	err, validator := New(fields)
	if err != nil {
		panic(err.Error())
	}

	isValid, errors := validator.Validate(req.MultipartForm.File)
	if isValid {
		// save file...
	} else {
		// display errors
		tplVars := make(map[string]interface{})
		tplVars["FormErrors"] = errors
		err = tpl.ExecuteTemplate(w, "index.gohtml", tplVars)
	}
}

Validation rules

  • NewField(required bool, singleFile bool, rules []Rule)
  • FileSize(min, max uint64)
  • MimeTypes(list []string)
  • MinPixelSize(width, height uint32)
  • MaxPixelSize(width, height uint32)

See the file 'validate_test.go' for examples of how to use the validation rules.

FAQs

Package last updated on 29 Dec 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc