csvvalidator

package module
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 19, 2023 License: MIT Imports: 3 Imported by: 0

README

csvvalidator

GO Actions Status Go Reference Go Report Card License

csvvalidator provide validation of csv rows according to set up rules.

Documentation

Overview

Package csvvalidator provide validation of csv rows according to set up rules.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// RuleNotEmpty defines a rule for field that require it to be not empty.
	RuleNotEmpty = Rule{
		MaxLength:       0,
		MinLength:       1,
		RestrictedChars: nil,
	}
	// RuleShouldBeEmpty defines a rule for field that require it to be empty.
	RuleShouldBeEmpty = Rule{
		MaxLength:       0,
		MinLength:       0,
		RestrictedChars: nil,
	}
)

Functions

This section is empty.

Types

type Column

type Column struct {
	Number uint
	Name   string
}

Column represents csv column.

func NewColumn

func NewColumn(number uint, name ...string) Column

NewColumn creates new Column with passed column number, name of column is optional.

Example
package main

import (
	"fmt"

	"github.com/obalunenko/csvvalidator"
)

func main() {
	clientNameCol := csvvalidator.NewColumn(0, "Client Name")
	fmt.Print(clientNameCol.String())
}
Output:

0:Client Name

func (*Column) String

func (c *Column) String() string

type Row

type Row struct {
	ColumnsTotalNum uint
	ColumnsRules    ValidationRules
}

Row represent csv row: - total columns number for row - validation rules for each column.

func (Row) ValidateRow

func (row Row) ValidateRow(rec []string) error

ValidateRow validates passed row.

type Rule

type Rule struct {
	MaxLength       uint // if 0 - no limit
	MinLength       uint // if 0 - no limit
	RestrictedChars []string
}

Rule represent column row validation rule.

type ValidationRules

type ValidationRules map[Column]Rule

ValidationRules represent rules for columns validation, counting of columns in row starts from 0.

func (ValidationRules) ValidateRow

func (rules ValidationRules) ValidateRow(rec []string) error

ValidateRow validates passed row, counting of columns in row starts from 0.

Example
package main

import (
	"fmt"

	"github.com/obalunenko/csvvalidator"
)

func main() {
	// initialise columns
	var (
		clientNameCol        = csvvalidator.NewColumn(0, "Client Name")
		transactionDateCol   = csvvalidator.NewColumn(1, "Transaction Date")
		transactionAmountCol = csvvalidator.NewColumn(2, "Transaction Amount")
		debitCreditCodeCol   = csvvalidator.NewColumn(3, "Debit Credit Code")
		customerIDCol        = csvvalidator.NewColumn(4, "Customer ID")

		// set total columns number
		totalColNum = uint(5)
	)

	// set up validation rules
	var rowRules = csvvalidator.ValidationRules{
		clientNameCol: csvvalidator.Rule{
			MaxLength:       4,
			MinLength:       4,
			RestrictedChars: nil,
		},
		transactionDateCol: csvvalidator.Rule{
			MaxLength:       10,
			MinLength:       10,
			RestrictedChars: nil,
		},
		transactionAmountCol: csvvalidator.Rule{
			MaxLength:       14,
			MinLength:       4,
			RestrictedChars: nil,
		},
		debitCreditCodeCol: csvvalidator.Rule{
			MaxLength:       1,
			MinLength:       1,
			RestrictedChars: nil,
		},
		customerIDCol: csvvalidator.Rule{
			MaxLength:       10,
			MinLength:       1,
			RestrictedChars: nil,
		},
	}

	// create csv row ([]string)
	var testDataRow = make([]string, totalColNum)
	testDataRow[clientNameCol.Number] = "Test Client Name too long" // exceed maxlength
	testDataRow[transactionDateCol.Number] = "2019/05/30"
	testDataRow[transactionAmountCol.Number] = "100.45"
	testDataRow[debitCreditCodeCol.Number] = "C"
	testDataRow[customerIDCol.Number] = "ID"

	// validate row
	err := rowRules.ValidateRow(testDataRow)
	fmt.Print(err)
}
Output:

invalid column [row:[Test Client Name too long 2019/05/30 100.45 C ID]; column:0:Client Name]: invalid length [column:[Test Client Name too long]; has len:[25]; should be:[4]]

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL