diff --git a/README.md b/README.md index 527b438..91ac042 100644 --- a/README.md +++ b/README.md @@ -1254,7 +1254,7 @@ import "github.com/duke-git/lancet/v2/validator" [[play](https://go.dev/play/p/LzaKocSV79u)] - **IsJSON** : check if the string is valid JSON. [[doc](https://github.com/duke-git/lancet/blob/main/docs/validator.md#IsJSON)] - [[play](https://go.dev/play/p/sRS6c4K8jGk)] + [[play](https://go.dev/play/p/8Kip1Itjiil)] - **IsRegexMatch** : check if the string match the regexp. [[doc](https://github.com/duke-git/lancet/blob/main/docs/validator.md#IsRegexMatch)] [[play](https://go.dev/play/p/z_XeZo_litG)] diff --git a/README_zh-CN.md b/README_zh-CN.md index f07e1b2..ccfae27 100644 --- a/README_zh-CN.md +++ b/README_zh-CN.md @@ -1255,7 +1255,7 @@ import "github.com/duke-git/lancet/v2/validator" [[play](https://go.dev/play/p/LzaKocSV79u)] - **IsJSON** : 验证字符串是否是有效 json。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/validator_zh-CN.md#IsJSON)] - [[play](https://go.dev/play/p/sRS6c4K8jGk)] + [[play](https://go.dev/play/p/8Kip1Itjiil)] - **IsRegexMatch** : 验证字符串是否可以匹配正则表达式。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/validator_zh-CN.md#IsRegexMatch)] [[play](https://go.dev/play/p/z_XeZo_litG)] diff --git a/docs/validator.md b/docs/validator.md index 3809aff..112d2e2 100644 --- a/docs/validator.md +++ b/docs/validator.md @@ -38,11 +38,14 @@ import ( - [IsDns](#IsDns) - [IsEmail](#IsEmail) - [IsEmptyString](#IsEmptyString) +- [IsInt](#IsInt) +- [IsFloat](#IsFloat) +- [IsNumber](#IsNumber) +- [IsIntStr](#IsIntStr) - [IsFloatStr](#IsFloatStr) - [IsNumberStr](#IsNumberStr) - [IsJSON](#IsJSON) - [IsRegexMatch](#IsRegexMatch) -- [IsIntStr](#IsIntStr) - [IsIp](#IsIp) - [IsIpV4](#IsIpV4) - [IsIpV6](#IsIpV6) @@ -589,6 +592,154 @@ func main() { } ``` +### IsInt + +
Check if the value is integer(int, unit) or not.
+ +Signature: + +```go +func IsInt(v any) bool +``` + +Example: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/validator" +) + +func main() { + result1 := validator.IsInt("") + result2 := validator.IsInt("3") + result3 := validator.IsInt(0.1) + result4 := validator.IsInt(0) + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + fmt.Println(result4) + + // Output: + // false + // false + // false + // true +} +``` + +### IsInt + +Check if the value is float(float32, float34) or not.
+ +Signature: + +```go +func IsFloat(v any) bool +``` + +Example: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/validator" +) + +func main() { + result1 := validator.IsFloat("") + result2 := validator.IsFloat("3") + result3 := validator.IsFloat(0) + result4 := validator.IsFloat(0.1) + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + fmt.Println(result4) + + // Output: + // false + // false + // false + // true +} +``` + +### IsNumber + +Check if the value is number(integer, float) or not.
+ +Signature: + +```go +func IsNumber(v any) bool +``` + +Example: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/validator" +) + +func main() { + result1 := validator.IsNumber("") + result2 := validator.IsNumber("3") + result3 := validator.IsNumber(0.1) + result4 := validator.IsNumber(0) + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + fmt.Println(result4) + + // Output: + // false + // false + // true + // true +} +``` + +### IsIntStr + +Check if the string can convert to a integer.
+ +Signature: + +```go +func IsIntStr(s string) bool +``` + +Example: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/validator" +) + +func main() { + result1 := validator.IsIntStr("+3") + result2 := validator.IsIntStr("-3") + result3 := validator.IsIntStr("3.") + result4 := validator.IsIntStr("abc") + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + fmt.Println(result4) + + // Output: + // true + // true + // false + // false +} +``` + ### IsFloatStrCheck if the string can convert to a float.
@@ -684,8 +835,8 @@ import ( func main() { result1 := validator.IsJSON("{}") result2 := validator.IsJSON("{\"name\": \"test\"}") - result3 := validator.IsIntStr("") - result4 := validator.IsIntStr("abc") + result3 := validator.IsJSON("") + result4 := validator.IsJSON("abc") fmt.Println(result1) fmt.Println(result2) @@ -731,43 +882,6 @@ func main() { } ``` -### IsIntStr - -Check if the string can convert to a integer.
- -Signature: - -```go -func IsIntStr(s string) bool -``` - -Example: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/validator" -) - -func main() { - result1 := validator.IsIntStr("+3") - result2 := validator.IsIntStr("-3") - result3 := validator.IsIntStr("3.") - result4 := validator.IsIntStr("abc") - - fmt.Println(result1) - fmt.Println(result2) - fmt.Println(result3) - fmt.Println(result4) - - // Output: - // true - // true - // false - // false -} -``` - ### IsIpCheck if the string is a ip address.
@@ -1033,7 +1147,6 @@ func main() { } ``` - ### IsPrintableChecks if string is all printable chars.
@@ -1072,4 +1185,4 @@ func main() { // true // false } -``` \ No newline at end of file +``` diff --git a/docs/validator_zh-CN.md b/docs/validator_zh-CN.md index b55a765..d59cf47 100644 --- a/docs/validator_zh-CN.md +++ b/docs/validator_zh-CN.md @@ -38,11 +38,14 @@ import ( - [IsDns](#IsDns) - [IsEmail](#IsEmail) - [IsEmptyString](#IsEmptyString) +- [IsInt](#IsInt) +- [IsFloat](#IsFloat) +- [IsNumber](#IsNumber) +- [IsIntStr](#IsIntStr) - [IsFloatStr](#IsFloatStr) - [IsNumberStr](#IsNumberStr) - [IsJSON](#IsJSON) - [IsRegexMatch](#IsRegexMatch) -- [IsIntStr](#IsIntStr) - [IsIp](#IsIp) - [IsIpV4](#IsIpV4) - [IsIpV6](#IsIpV6) @@ -589,6 +592,155 @@ func main() { } ``` +### IsInt + +验证参数是否是整数(int, unit)。
+ +函数签名: + +```go +func IsInt(v any) bool +``` + +示例: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/validator" +) + +func main() { + result1 := validator.IsInt("") + result2 := validator.IsInt("3") + result3 := validator.IsInt(0.1) + result4 := validator.IsInt(0) + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + fmt.Println(result4) + + // Output: + // false + // false + // false + // true +} +``` + +### IsInt + +验证参数是否是浮点数((float32, float34)。
+ +函数签名: + +```go +func IsFloat(v any) bool +``` + +示例: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/validator" +) + +func main() { + result1 := validator.IsFloat("") + result2 := validator.IsFloat("3") + result3 := validator.IsFloat(0) + result4 := validator.IsFloat(0.1) + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + fmt.Println(result4) + + // Output: + // false + // false + // false + // true +} +``` + +### IsNumber + +验证参数是否是数字(integer or float)
+ +函数签名: + +```go +func IsNumber(v any) bool +``` + +示例: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/validator" +) + +func main() { + result1 := validator.IsNumber("") + result2 := validator.IsNumber("3") + result3 := validator.IsNumber(0.1) + result4 := validator.IsNumber(0) + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + fmt.Println(result4) + + // Output: + // false + // false + // true + // true +} +``` + + +### IsIntStr + +验证字符串是否是可以转换为整数
+ +函数签名: + +```go +func IsIntStr(s string) bool +``` + +示例: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/validator" +) + +func main() { + result1 := validator.IsIntStr("+3") + result2 := validator.IsIntStr("-3") + result3 := validator.IsIntStr("3.") + result4 := validator.IsIntStr("abc") + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + fmt.Println(result4) + + // Output: + // true + // true + // false + // false +} +``` + ### IsFloatStr验证字符串是否是可以转换为浮点数
@@ -684,8 +836,8 @@ import ( func main() { result1 := validator.IsJSON("{}") result2 := validator.IsJSON("{\"name\": \"test\"}") - result3 := validator.IsIntStr("") - result4 := validator.IsIntStr("abc") + result3 := validator.IsJSON("") + result4 := validator.IsJSON("abc") fmt.Println(result1) fmt.Println(result2) @@ -731,42 +883,7 @@ func main() { } ``` -### IsIntStr -验证字符串是否是可以转换为整数
- -函数签名: - -```go -func IsIntStr(s string) bool -``` - -示例: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/validator" -) - -func main() { - result1 := validator.IsIntStr("+3") - result2 := validator.IsIntStr("-3") - result3 := validator.IsIntStr("3.") - result4 := validator.IsIntStr("abc") - - fmt.Println(result1) - fmt.Println(result2) - fmt.Println(result3) - fmt.Println(result4) - - // Output: - // true - // true - // false - // false -} -``` ### IsIp diff --git a/validator/validator.go b/validator/validator.go index fc26b4b..bd01eb3 100644 --- a/validator/validator.go +++ b/validator/validator.go @@ -112,7 +112,7 @@ func ContainLetter(str string) bool { } // IsJSON checks if the string is valid JSON. -// Play: https://go.dev/play/p/sRS6c4K8jGk +// Play: https://go.dev/play/p/8Kip1Itjiil func IsJSON(str string) bool { var js json.RawMessage return json.Unmarshal([]byte(str), &js) == nil @@ -370,7 +370,7 @@ func IsGBK(data []byte) bool { return true } -// IsNumberStr check if the value is Number(integer, float) or not. +// IsNumberStr check if the value is number(integer, float) or not. // Play: todo func IsNumber(v any) bool { return IsInt(v) || IsFloat(v) diff --git a/validator/validator_example_test.go b/validator/validator_example_test.go index e59a0b8..2a9e754 100644 --- a/validator/validator_example_test.go +++ b/validator/validator_example_test.go @@ -285,8 +285,8 @@ func ExampleIsIntStr() { func ExampleIsJSON() { result1 := IsJSON("{}") result2 := IsJSON("{\"name\": \"test\"}") - result3 := IsIntStr("") - result4 := IsIntStr("abc") + result3 := IsJSON("") + result4 := IsJSON("abc") fmt.Println(result1) fmt.Println(result2)