Remove 3rd party package for SQL field validation
This commit is contained in:
parent
b988b2c890
commit
d163db967f
5 changed files with 93 additions and 16 deletions
|
@ -19,8 +19,6 @@ import:
|
|||
subpackages:
|
||||
- mw
|
||||
- package: github.com/abcum/rixxdb
|
||||
- package: github.com/asaskevich/govalidator
|
||||
version: ^8.0.0
|
||||
- package: github.com/dgrijalva/jwt-go
|
||||
version: ^3.1.0
|
||||
- package: github.com/elithrar/simple-scrypt
|
||||
|
|
43
util/chck/chck.go
Normal file
43
util/chck/chck.go
Normal file
|
@ -0,0 +1,43 @@
|
|||
// Copyright © 2016 Abcum Ltd
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package chck
|
||||
|
||||
func IsBase64(s string) bool {
|
||||
return rBase64.MatchString(s)
|
||||
}
|
||||
|
||||
func IsDomain(s string) bool {
|
||||
return rDomain.MatchString(s)
|
||||
}
|
||||
|
||||
func IsEmail(s string) bool {
|
||||
return rEmail.MatchString(s)
|
||||
}
|
||||
|
||||
func IsLatitude(s string) bool {
|
||||
return rLatitude.MatchString(s)
|
||||
}
|
||||
|
||||
func IsLongitude(s string) bool {
|
||||
return rLongitude.MatchString(s)
|
||||
}
|
||||
|
||||
func IsPhone(s string) bool {
|
||||
return rPhone.MatchString(s)
|
||||
}
|
||||
|
||||
func IsUUID(s string) bool {
|
||||
return rUUID.MatchString(s)
|
||||
}
|
37
util/chck/regex.go
Normal file
37
util/chck/regex.go
Normal file
|
@ -0,0 +1,37 @@
|
|||
// Copyright © 2016 Abcum Ltd
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package chck
|
||||
|
||||
import "regexp"
|
||||
|
||||
const (
|
||||
sBase64 string = "^(?:[A-Za-z0-9+\\/]{4})*(?:[A-Za-z0-9+\\/]{2}==|[A-Za-z0-9+\\/]{3}=|[A-Za-z0-9+\\/]{4})$"
|
||||
sDomain string = `^([a-zA-Z0-9_]{1}[a-zA-Z0-9_-]{0,62}){1}(\.[a-zA-Z0-9_]{1}[a-zA-Z0-9_-]{0,62})*[\._]?$`
|
||||
sEmail string = "^(((([a-zA-Z]|\\d|[!#\\$%&'\\*\\+\\-\\/=\\?\\^_`{\\|}~]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])+(\\.([a-zA-Z]|\\d|[!#\\$%&'\\*\\+\\-\\/=\\?\\^_`{\\|}~]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])+)*)|((\\x22)((((\\x20|\\x09)*(\\x0d\\x0a))?(\\x20|\\x09)+)?(([\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x7f]|\\x21|[\\x23-\\x5b]|[\\x5d-\\x7e]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])|(\\([\\x01-\\x09\\x0b\\x0c\\x0d-\\x7f]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}]))))*(((\\x20|\\x09)*(\\x0d\\x0a))?(\\x20|\\x09)+)?(\\x22)))@((([a-zA-Z]|\\d|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])|(([a-zA-Z]|\\d|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])([a-zA-Z]|\\d|-|\\.|_|~|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])*([a-zA-Z]|\\d|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])))\\.)+(([a-zA-Z]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])|(([a-zA-Z]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])([a-zA-Z]|\\d|-|_|~|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])*([a-zA-Z]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])))\\.?$"
|
||||
sLatitude string = "^[-+]?([1-8]?\\d(\\.\\d+)?|90(\\.0+)?)$"
|
||||
sLongitude string = "^[-+]?(180(\\.0+)?|((1[0-7]\\d)|([1-9]?\\d))(\\.\\d+)?)$"
|
||||
sPhone string = `^[\s\d\+\-\(\)]+$`
|
||||
sUUID string = "^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$"
|
||||
)
|
||||
|
||||
var (
|
||||
rBase64 = regexp.MustCompile(sBase64)
|
||||
rDomain = regexp.MustCompile(sDomain)
|
||||
rEmail = regexp.MustCompile(sEmail)
|
||||
rLatitude = regexp.MustCompile(sLatitude)
|
||||
rLongitude = regexp.MustCompile(sLongitude)
|
||||
rPhone = regexp.MustCompile(sPhone)
|
||||
rUUID = regexp.MustCompile(sUUID)
|
||||
)
|
|
@ -20,8 +20,7 @@ import (
|
|||
"time"
|
||||
|
||||
"github.com/abcum/surreal/sql"
|
||||
|
||||
"github.com/asaskevich/govalidator"
|
||||
"github.com/abcum/surreal/util/chck"
|
||||
)
|
||||
|
||||
func toNumber(str string) (float64, error) {
|
||||
|
@ -122,7 +121,7 @@ func ConvertTo(t, k string, obj interface{}) (val interface{}, err error) {
|
|||
|
||||
func ConvertToUuid(obj interface{}) (val string, err error) {
|
||||
val = fmt.Sprintf("%v", obj)
|
||||
if !govalidator.IsUUID(val) {
|
||||
if !chck.IsUUID(val) {
|
||||
err = fmt.Errorf("Expected a UUID, but found '%v'", obj)
|
||||
}
|
||||
return
|
||||
|
@ -130,15 +129,15 @@ func ConvertToUuid(obj interface{}) (val string, err error) {
|
|||
|
||||
func ConvertToEmail(obj interface{}) (val string, err error) {
|
||||
val = fmt.Sprintf("%v", obj)
|
||||
if !govalidator.IsEmail(val) {
|
||||
if !chck.IsEmail(val) {
|
||||
err = fmt.Errorf("Expected an email address, but found '%v'", obj)
|
||||
}
|
||||
return govalidator.NormalizeEmail(val)
|
||||
return
|
||||
}
|
||||
|
||||
func ConvertToPhone(obj interface{}) (val string, err error) {
|
||||
val = fmt.Sprintf("%v", obj)
|
||||
if !govalidator.Matches(val, `^[\s\d\+\-\(\)]+$`) {
|
||||
if !chck.IsPhone(val) {
|
||||
err = fmt.Errorf("Expected a phone number, but found '%v'", obj)
|
||||
}
|
||||
return
|
||||
|
@ -164,7 +163,7 @@ func ConvertToObject(obj interface{}) (val map[string]interface{}, err error) {
|
|||
|
||||
func ConvertToDomain(obj interface{}) (val string, err error) {
|
||||
val = fmt.Sprintf("%v", obj)
|
||||
if !govalidator.IsDNSName(val) {
|
||||
if !chck.IsDomain(val) {
|
||||
err = fmt.Errorf("Expected a domain name, but found '%v'", obj)
|
||||
}
|
||||
return
|
||||
|
@ -172,7 +171,7 @@ func ConvertToDomain(obj interface{}) (val string, err error) {
|
|||
|
||||
func ConvertToBase64(obj interface{}) (val string, err error) {
|
||||
val = fmt.Sprintf("%v", obj)
|
||||
if !govalidator.IsBase64(val) {
|
||||
if !chck.IsBase64(val) {
|
||||
err = fmt.Errorf("Expected base64 data, but found '%v'", obj)
|
||||
}
|
||||
return
|
||||
|
@ -242,18 +241,18 @@ func ConvertToDatetime(obj interface{}) (val time.Time, err error) {
|
|||
|
||||
func ConvertToLatitude(obj interface{}) (val float64, err error) {
|
||||
str := fmt.Sprintf("%v", obj)
|
||||
if !govalidator.IsLatitude(str) {
|
||||
if !chck.IsLatitude(str) {
|
||||
err = fmt.Errorf("Expected a latitude value, but found '%v'", obj)
|
||||
}
|
||||
return govalidator.ToFloat(str)
|
||||
return toNumber(str)
|
||||
}
|
||||
|
||||
func ConvertToLongitude(obj interface{}) (val float64, err error) {
|
||||
str := fmt.Sprintf("%v", obj)
|
||||
if !govalidator.IsLongitude(str) {
|
||||
if !chck.IsLongitude(str) {
|
||||
err = fmt.Errorf("Expected a longitude value, but found '%v'", obj)
|
||||
}
|
||||
return govalidator.ToFloat(str)
|
||||
return toNumber(str)
|
||||
}
|
||||
|
||||
func ConvertToRecord(obj interface{}, tb string) (val *sql.Thing, err error) {
|
||||
|
|
|
@ -18,7 +18,7 @@ import (
|
|||
"context"
|
||||
"strings"
|
||||
|
||||
"github.com/asaskevich/govalidator"
|
||||
"github.com/abcum/surreal/util/chck"
|
||||
)
|
||||
|
||||
func emailUser(ctx context.Context, args ...interface{}) (string, error) {
|
||||
|
@ -39,5 +39,5 @@ func emailDomain(ctx context.Context, args ...interface{}) (string, error) {
|
|||
|
||||
func emailValid(ctx context.Context, args ...interface{}) (bool, error) {
|
||||
v, _ := ensureString(args[0])
|
||||
return govalidator.IsEmail(v), nil
|
||||
return chck.IsEmail(v), nil
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue