Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@

It is validator about official of code documents from Russia in Go

## Alert

It is not production ready public API! It is possible API will be change it the future. To be attentive for this.

## Usage

* go get github.com/sshaplygin/ru-doc-code
``` bash
go get github.com/sshaplygin/ru-doc-code
```

### Example

Expand All @@ -22,7 +28,7 @@ import (

isValid, err := inn.Validate("526317984689")
if err != nil {
log.Fatal(err)
log.Error(err)
}
if isValid {
log.Println("INN is valid")
Expand All @@ -31,9 +37,9 @@ if isValid {

## Documentation

-Info about INN - [ИНН](https://ru.wikipedia.org/wiki/%D0%98%D0%B4%D0%B5%D0%BD%D1%82%D0%B8%D1%84%D0%B8%D0%BA%D0%B0%D1%86%D0%B8%D0%BE%D0%BD%D0%BD%D1%8B%D0%B9_%D0%BD%D0%BE%D0%BC%D0%B5%D1%80_%D0%BD%D0%B0%D0%BB%D0%BE%D0%B3%D0%BE%D0%BF%D0%BB%D0%B0%D1%82%D0%B5%D0%BB%D1%8C%D1%89%D0%B8%D0%BA%D0%B0) \
-Info about SNILS - [СНИЛС](http://www.consultant.ru/document/cons_doc_LAW_124607/68ac3b2d1745f9cc7d4332b63c2818ca5d5d20d0/) \
-Info about OGRN - [ОГРН](https://ru.wikipedia.org/wiki/%D0%9E%D1%81%D0%BD%D0%BE%D0%B2%D0%BD%D0%BE%D0%B9_%D0%B3%D0%BE%D1%81%D1%83%D0%B4%D0%B0%D1%80%D1%81%D1%82%D0%B2%D0%B5%D0%BD%D0%BD%D1%8B%D0%B9_%D1%80%D0%B5%D0%B3%D0%B8%D1%81%D1%82%D1%80%D0%B0%D1%86%D0%B8%D0%BE%D0%BD%D0%BD%D1%8B%D0%B9_%D0%BD%D0%BE%D0%BC%D0%B5%D1%80) \
-Info about OGRNIP - [ОГРНИП](http://www.temabiz.com/terminy/chto-takoe-ogrnip.html) \
-Info about BIK - [БИК](https://ru.wikipedia.org/wiki/%D0%91%D0%B0%D0%BD%D0%BA%D0%BE%D0%B2%D1%81%D0%BA%D0%B8%D0%B9_%D0%B8%D0%B4%D0%B5%D0%BD%D1%82%D0%B8%D1%84%D0%B8%D0%BA%D0%B0%D1%86%D0%B8%D0%BE%D0%BD%D0%BD%D1%8B%D0%B9_%D0%BA%D0%BE%D0%B4) \
-Info about KPP - [КПП](https://dic.academic.ru/dic.nsf/ruwiki/239834)
* Info about INN - [ИНН](https://ru.wikipedia.org/wiki/%D0%98%D0%B4%D0%B5%D0%BD%D1%82%D0%B8%D1%84%D0%B8%D0%BA%D0%B0%D1%86%D0%B8%D0%BE%D0%BD%D0%BD%D1%8B%D0%B9_%D0%BD%D0%BE%D0%BC%D0%B5%D1%80_%D0%BD%D0%B0%D0%BB%D0%BE%D0%B3%D0%BE%D0%BF%D0%BB%D0%B0%D1%82%D0%B5%D0%BB%D1%8C%D1%89%D0%B8%D0%BA%D0%B0) \
* Info about SNILS - [СНИЛС](http://www.consultant.ru/document/cons_doc_LAW_124607/68ac3b2d1745f9cc7d4332b63c2818ca5d5d20d0/) \
* Info about OGRN - [ОГРН](https://ru.wikipedia.org/wiki/%D0%9E%D1%81%D0%BD%D0%BE%D0%B2%D0%BD%D0%BE%D0%B9_%D0%B3%D0%BE%D1%81%D1%83%D0%B4%D0%B0%D1%80%D1%81%D1%82%D0%B2%D0%B5%D0%BD%D0%BD%D1%8B%D0%B9_%D1%80%D0%B5%D0%B3%D0%B8%D1%81%D1%82%D1%80%D0%B0%D1%86%D0%B8%D0%BE%D0%BD%D0%BD%D1%8B%D0%B9_%D0%BD%D0%BE%D0%BC%D0%B5%D1%80) \
* Info about OGRNIP - [ОГРНИП](http://www.temabiz.com/terminy/chto-takoe-ogrnip.html) \
* Info about BIK - [БИК](https://ru.wikipedia.org/wiki/%D0%91%D0%B0%D0%BD%D0%BA%D0%BE%D0%B2%D1%81%D0%BA%D0%B8%D0%B9_%D0%B8%D0%B4%D0%B5%D0%BD%D1%82%D0%B8%D1%84%D0%B8%D0%BA%D0%B0%D1%86%D0%B8%D0%BE%D0%BD%D0%BD%D1%8B%D0%B9_%D0%BA%D0%BE%D0%B4) \
* Info about KPP - [КПП](https://dic.academic.ru/dic.nsf/ruwiki/239834)
21 changes: 11 additions & 10 deletions bik/bik.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,27 @@ package bik
import (
"strconv"

ru_doc_code "github.com/sshaplygin/ru-doc-code"
"github.com/sshaplygin/ru-doc-code/models"
"github.com/sshaplygin/ru-doc-code/utils"
)

// Validate check to valid BIK format
// example valid format is 044525225
func Validate(bik string) (bool, error) {
if len(bik) != 9 {
name, err := ru_doc_code.GetModuleName()
if err != nil {
return false, err
}
return false, &ru_doc_code.CommonError{
Method: name,
Err: ru_doc_code.ErrInvalidLength,
return false, &models.CommonError{
Method: packageName,
Err: models.ErrInvalidLength,
}
}

bikArr, err := ru_doc_code.StrToArr(bik)
bikArr, err := utils.StrToArr(bik)
if err != nil {
return false, err
}

if bikArr[0] != 0 || bikArr[1] != 4 {
return false, ru_doc_code.ErrInvalidBIKCountryCode
return false, ErrInvalidCountryCode
}

// special code
Expand All @@ -39,3 +36,7 @@ func Validate(bik string) (bool, error) {

return code >= 50 && code < 1000, nil
}

func Generate() string {
panic("not implemented!")
}
31 changes: 22 additions & 9 deletions bik/bik_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,28 @@ import (

"github.com/stretchr/testify/assert"

ru_doc_code "github.com/sshaplygin/ru-doc-code"
"github.com/sshaplygin/ru-doc-code/models"
)

func TestValidate(t *testing.T) {
t.Parallel()

type testCase struct {
Code string
IsValid bool
Error error
}

t.Run("invalid bik length", func(t *testing.T) {
testCases := []ru_doc_code.TestCodeCase{
testCases := []testCase{
{
Code: "1234567888776",
Error: ru_doc_code.ErrInvalidLength,
Error: models.ErrInvalidLength,
IsValid: false,
},
{
Code: "044525",
Error: ru_doc_code.ErrInvalidLength,
Error: models.ErrInvalidLength,
IsValid: false,
},
{
Expand All @@ -35,6 +41,7 @@ func TestValidate(t *testing.T) {
IsValid: true,
},
}

for i, tc := range testCases {
tc := tc

Expand All @@ -49,25 +56,31 @@ func TestValidate(t *testing.T) {
})

t.Run("invalid bik value", func(t *testing.T) {
testCases := []ru_doc_code.TestCodeCase{
type testCase struct {
Code string
IsValid bool
Error error
}

testCases := []testCase{
{
Code: "0445?5226",
Error: ru_doc_code.ErrInvalidValue,
Error: models.ErrInvalidValue,
IsValid: false,
},
{
Code: "054525225",
Error: ru_doc_code.ErrInvalidBIKCountryCode,
Error: ErrInvalidCountryCode,
IsValid: false,
},
{
Code: "104525225",
Error: ru_doc_code.ErrInvalidBIKCountryCode,
Error: ErrInvalidCountryCode,
IsValid: false,
},
{
Code: "044#55#25",
Error: ru_doc_code.ErrInvalidValue,
Error: models.ErrInvalidValue,
IsValid: false,
},
{
Expand Down
8 changes: 8 additions & 0 deletions bik/errros.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package bik

import "errors"

var (
// ErrInvalidCountryCode invalid bik code country
ErrInvalidCountryCode = errors.New("invalid bik country code")
)
3 changes: 3 additions & 0 deletions bik/models.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package bik

const packageName = "bik"
57 changes: 0 additions & 57 deletions errors.go

This file was deleted.

25 changes: 11 additions & 14 deletions inn/inn.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import (
"strconv"
"strings"

ru_doc_code "github.com/sshaplygin/ru-doc-code"
"github.com/sshaplygin/ru-doc-code/models"
"github.com/sshaplygin/ru-doc-code/utils"
)

const (
Expand All @@ -13,8 +14,8 @@ const (
)

type INN struct {
Code ru_doc_code.TaxRegionCode
SerialNumber ru_doc_code.SerialNumber
Code models.TaxRegionCode
SerialNumber models.SerialNumber
Hash10 uint
Hash11 uint
Hash12 uint
Expand All @@ -24,13 +25,9 @@ type INN struct {
// example: input format is 7707083893
func Validate(inn string) (bool, error) {
if len(inn) != lengthLegal && len(inn) != lengthPhysical {
name, err := ru_doc_code.GetModuleName()
if err != nil {
return false, err
}
return false, &ru_doc_code.CommonError{
Method: name,
Err: ru_doc_code.ErrInvalidLength,
return false, &models.CommonError{
Method: packageName,
Err: models.ErrInvalidLength,
}
}

Expand All @@ -47,15 +44,15 @@ func Validate(inn string) (bool, error) {

// GenerateLegal generate legal type inn string value.
func GenerateLegal() string {
inn := strconv.FormatInt(ru_doc_code.RandomDigits(9), 10)
inn := strconv.FormatInt(utils.RandomDigits(9), 10)
innArr, _ := transformInn(inn)

return inn + strconv.Itoa(hash10(innArr))
}

// GeneratePhysical generate physical type inn string value.
func GeneratePhysical() string {
inn := strconv.FormatInt(ru_doc_code.RandomDigits(10), 10)
inn := strconv.FormatInt(utils.RandomDigits(10), 10)
innArr, _ := transformInn(inn)

hash1Num := hash11(innArr)
Expand All @@ -75,7 +72,7 @@ func transformInn(inn string) ([]int, error) {
for _, str := range innNumbers {
number, err := strconv.Atoi(str)
if err != nil {
return nil, ru_doc_code.ErrInvalidValue
return nil, models.ErrInvalidValue
}
innArr = append(innArr, number)
}
Expand All @@ -85,7 +82,7 @@ func transformInn(inn string) ([]int, error) {

// Generate generate random type inn string value.
func Generate() string {
if ru_doc_code.RandomDigits(1)%2 == 1 {
if utils.RandomDigits(1)%2 == 1 {
return GeneratePhysical()
}

Expand Down
31 changes: 22 additions & 9 deletions inn/inn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,29 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

ru_doc_code "github.com/sshaplygin/ru-doc-code"
"github.com/sshaplygin/ru-doc-code/models"
"github.com/sshaplygin/ru-doc-code/utils"
)

func TestValidate(t *testing.T) {
t.Parallel()

t.Run("invalid inn length", func(t *testing.T) {
testCases := []ru_doc_code.TestCodeCase{
type testCase struct {
Code string
IsValid bool
Error error
}

testCases := []testCase{
{
Code: "12345678",
Error: ru_doc_code.ErrInvalidLength,
Error: models.ErrInvalidLength,
IsValid: false,
},
{
Code: "9876543211123",
Error: ru_doc_code.ErrInvalidLength,
Error: models.ErrInvalidLength,
IsValid: false,
},
{
Expand Down Expand Up @@ -50,20 +57,26 @@ func TestValidate(t *testing.T) {
})

t.Run("invalid inn value", func(t *testing.T) {
testCases := []ru_doc_code.TestCodeCase{
type testCase struct {
Code string
IsValid bool
Error error
}

testCases := []testCase{
{
Code: "77$7083893",
Error: ru_doc_code.ErrInvalidValue,
Error: models.ErrInvalidValue,
IsValid: false,
},
{
Code: "98754321N123",
Error: ru_doc_code.ErrInvalidValue,
Error: models.ErrInvalidValue,
IsValid: false,
},
{
Code: "9854132d1123",
Error: ru_doc_code.ErrInvalidValue,
Error: models.ErrInvalidValue,
IsValid: false,
},
{
Expand Down Expand Up @@ -162,7 +175,7 @@ func TestGenerate(t *testing.T) {
for _, tc := range tests {
tc := tc

digits = ru_doc_code.RandomDigits(tc.len)
digits = utils.RandomDigits(tc.len)
assert.True(t, digits >= tc.min && digits <= tc.max)
}
})
Expand Down
3 changes: 3 additions & 0 deletions inn/models.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package inn

const packageName = "packageName"
8 changes: 8 additions & 0 deletions kpp/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package kpp

import "errors"

var (
// ErrRegistrationReasonCode invalid registration reason code
ErrRegistrationReasonCode = errors.New("invalid registration reason code")
)
Loading