github.com/blend/go-sdk@v1.20220411.3/validate/string_test.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package validate
     9  
    10  import (
    11  	"strings"
    12  	"testing"
    13  
    14  	"github.com/blend/go-sdk/assert"
    15  	"github.com/blend/go-sdk/ex"
    16  	"github.com/blend/go-sdk/uuid"
    17  )
    18  
    19  func TestStringRequired(t *testing.T) {
    20  	assert := assert.New(t)
    21  
    22  	var verr error
    23  	verr = String(nil).Required()()
    24  	assert.NotNil(verr)
    25  	assert.Equal(ErrStringRequired, ErrCause(verr))
    26  
    27  	bad := ""
    28  	verr = String(&bad).Required()()
    29  	assert.NotNil(verr)
    30  	assert.Equal(ErrStringRequired, ErrCause(verr))
    31  
    32  	good := "ok!"
    33  	verr = String(&good).Required()()
    34  	assert.Nil(verr)
    35  }
    36  
    37  func TestStringForbidden(t *testing.T) {
    38  	assert := assert.New(t)
    39  
    40  	var verr error
    41  	verr = String(nil).Forbidden()()
    42  	assert.Nil(verr)
    43  
    44  	empty := ""
    45  	verr = String(&empty).Forbidden()()
    46  	assert.Nil(verr)
    47  
    48  	set := "ok!"
    49  	verr = String(&set).Forbidden()()
    50  	assert.NotNil(verr)
    51  	assert.Equal(ErrStringForbidden, ErrCause(verr))
    52  }
    53  
    54  func TestStringMin(t *testing.T) {
    55  	assert := assert.New(t)
    56  
    57  	var verr error
    58  	bad := "large"
    59  	verr = String(&bad).MinLen(3)()
    60  	assert.Nil(verr)
    61  
    62  	verr = String(nil).MinLen(3)()
    63  	assert.NotNil(verr)
    64  	assert.Equal(ErrValidation, ex.ErrClass(verr))
    65  	assert.Equal(ErrStringLengthMin, ErrCause(verr))
    66  
    67  	good := "a"
    68  	verr = String(&good).MinLen(3)()
    69  	assert.NotNil(verr)
    70  	assert.Equal(ErrValidation, ex.ErrClass(verr))
    71  	assert.Equal(ErrStringLengthMin, ErrCause(verr))
    72  }
    73  
    74  func TestStringMaxlen(t *testing.T) {
    75  	assert := assert.New(t)
    76  
    77  	var verr error
    78  	bad := "a"
    79  	verr = String(&bad).MaxLen(3)()
    80  	assert.Nil(verr)
    81  
    82  	verr = String(nil).MaxLen(3)()
    83  	assert.Nil(verr)
    84  
    85  	good := "large"
    86  	verr = String(&good).MaxLen(3)()
    87  	assert.NotNil(verr)
    88  	assert.Equal(ErrValidation, ex.ErrClass(verr))
    89  	assert.Equal(ErrStringLengthMax, ErrCause(verr))
    90  }
    91  
    92  func TestStringBetweenLen(t *testing.T) {
    93  	assert := assert.New(t)
    94  
    95  	var verr error
    96  	good := "ok"
    97  	verr = String(&good).BetweenLen(1, 3)()
    98  	assert.Nil(verr)
    99  
   100  	bad := "large"
   101  	verr = String(&bad).BetweenLen(1, 3)()
   102  	assert.NotNil(verr)
   103  	assert.Equal(ErrValidation, ex.ErrClass(verr))
   104  	assert.Equal(ErrStringLengthMax, ErrCause(verr))
   105  
   106  	verr = String(nil).BetweenLen(2, 5)()
   107  	assert.NotNil(verr)
   108  	assert.Equal(ErrValidation, ex.ErrClass(verr))
   109  	assert.Equal(ErrStringLengthMin, ErrCause(verr))
   110  
   111  	bad = "a"
   112  	verr = String(&bad).BetweenLen(2, 5)()
   113  	assert.NotNil(verr)
   114  	assert.Equal(ErrValidation, ex.ErrClass(verr))
   115  	assert.Equal(ErrStringLengthMin, ErrCause(verr))
   116  }
   117  
   118  func TestStringMatches(t *testing.T) {
   119  	assert := assert.New(t)
   120  
   121  	var verr error
   122  	good := "a foo"
   123  	verr = String(&good).Matches("foo$")()
   124  	assert.Nil(verr)
   125  
   126  	verr = String(nil).Matches("foo$")()
   127  	assert.NotNil(verr)
   128  	assert.Nil(ErrValue(verr))
   129  	assert.Equal(ErrStringMatches, ErrCause(verr))
   130  
   131  	bad := "foo not"
   132  	verr = String(&bad).Matches("foo$")()
   133  	assert.NotNil(verr)
   134  	assert.Equal(ErrStringMatches, ErrCause(verr))
   135  }
   136  
   137  func TestStringMatchesError(t *testing.T) {
   138  	assert := assert.New(t)
   139  
   140  	var err error
   141  	good := "a foo"
   142  	err = String(&good).Matches("((")() // this should be an invalid regex "(("
   143  	assert.NotNil(err)
   144  	assert.NotEqual(ErrValidation, ex.ErrClass(err))
   145  }
   146  
   147  func TestStringIsUpper(t *testing.T) {
   148  	assert := assert.New(t)
   149  
   150  	var verr error
   151  	good := "FOO"
   152  	verr = String(&good).IsUpper()()
   153  	assert.Nil(verr)
   154  
   155  	verr = String(nil).IsUpper()()
   156  	assert.NotNil(verr)
   157  	assert.Nil(ErrValue(verr))
   158  	assert.Equal(ErrStringIsUpper, ErrCause(verr))
   159  
   160  	bad := "FOo"
   161  	verr = String(&bad).IsUpper()()
   162  	assert.NotNil(verr)
   163  	assert.Equal(ErrStringIsUpper, ErrCause(verr))
   164  }
   165  
   166  func TestStringIsLower(t *testing.T) {
   167  	assert := assert.New(t)
   168  
   169  	var verr error
   170  	good := "foo"
   171  	verr = String(&good).IsLower()()
   172  	assert.Nil(verr)
   173  
   174  	verr = String(nil).IsLower()()
   175  	assert.NotNil(verr)
   176  	assert.Nil(ErrValue(verr))
   177  	assert.Equal(ErrStringIsLower, ErrCause(verr))
   178  
   179  	bad := "foO"
   180  	verr = String(&bad).IsLower()()
   181  	assert.NotNil(verr)
   182  	assert.Equal(ErrStringIsLower, ErrCause(verr))
   183  }
   184  
   185  func TestStringIsTitle(t *testing.T) {
   186  	assert := assert.New(t)
   187  
   188  	var verr error
   189  	good := strings.ToTitle("this is a test")
   190  	verr = String(&good).IsTitle()()
   191  	assert.Nil(verr)
   192  
   193  	verr = String(nil).IsTitle()()
   194  	assert.NotNil(verr)
   195  	assert.Nil(ErrValue(verr))
   196  	assert.Equal(ErrStringIsTitle, ErrCause(verr))
   197  
   198  	bad := "this is a test"
   199  	verr = String(&bad).IsTitle()()
   200  	assert.NotNil(verr)
   201  	assert.Equal(ErrStringIsTitle, ErrCause(verr))
   202  }
   203  
   204  func TestStringIsUUID(t *testing.T) {
   205  	assert := assert.New(t)
   206  
   207  	var verr error
   208  	good := uuid.V4().String()
   209  	verr = String(&good).IsUUID()()
   210  	assert.Nil(verr)
   211  
   212  	verr = String(nil).IsUUID()()
   213  	assert.NotNil(verr)
   214  	assert.NotNil(verr)
   215  	assert.Nil(ErrValue(verr))
   216  	assert.Equal(ErrStringIsUUID, ErrCause(verr))
   217  
   218  	good = uuid.V4().ToFullString()
   219  	verr = String(&good).IsUUID()()
   220  	assert.Nil(verr)
   221  
   222  	bad := "asldkfjaslkfjasdlfa"
   223  	verr = String(&bad).IsUUID()()
   224  	assert.NotNil(verr)
   225  	assert.Equal(ErrStringIsUUID, ErrCause(verr))
   226  }
   227  
   228  func TestStringIsEmail(t *testing.T) {
   229  	assert := assert.New(t)
   230  
   231  	var verr error
   232  	verr = String(nil).IsEmail()()
   233  	assert.NotNil(verr)
   234  	assert.Nil(ErrValue(verr))
   235  	assert.Equal(ErrStringIsEmail, ErrCause(verr))
   236  
   237  	good := "foo@bar.com"
   238  	verr = String(&good).IsEmail()()
   239  	assert.Nil(verr)
   240  
   241  	good = "foo@bar"
   242  	verr = String(&good).IsEmail()()
   243  	assert.Nil(verr)
   244  
   245  	good = "foo+foo@bar.com"
   246  	verr = String(&good).IsEmail()()
   247  	assert.Nil(verr)
   248  
   249  	bad := "this is a test"
   250  	verr = String(&bad).IsEmail()()
   251  	assert.NotNil(verr)
   252  	assert.Equal(ErrStringIsEmail, ErrCause(verr))
   253  }
   254  
   255  func TestStringIsURI(t *testing.T) {
   256  	assert := assert.New(t)
   257  
   258  	var verr error
   259  	verr = String(nil).IsURI()()
   260  	assert.NotNil(verr)
   261  	assert.Nil(ErrValue(verr))
   262  	assert.Equal(ErrStringIsURI, ErrCause(verr))
   263  
   264  	good := "https://foo.com"
   265  
   266  	verr = String(&good).IsURI()()
   267  	assert.Nil(verr)
   268  
   269  	bad := "this is a test"
   270  	verr = String(&bad).IsURI()()
   271  	assert.NotNil(verr)
   272  	assert.Equal(bad, ErrValue(verr))
   273  	assert.Equal(ErrStringIsURI, ErrCause(verr))
   274  }
   275  
   276  func TestStringIsIP(t *testing.T) {
   277  	assert := assert.New(t)
   278  
   279  	var verr error
   280  	verr = String(nil).IsIP()()
   281  	assert.NotNil(verr)
   282  	assert.Nil(ErrValue(verr))
   283  	assert.Equal(ErrStringIsIP, ErrCause(verr))
   284  
   285  	good := "127.0.0.1"
   286  	verr = String(&good).IsIP()()
   287  	assert.Nil(verr)
   288  	good = "172.217.0.46"
   289  	verr = String(&good).IsIP()()
   290  	assert.Nil(verr)
   291  	good = "2607:f8b0:4005:802::200e"
   292  	verr = String(&good).IsIP()()
   293  	assert.Nil(verr)
   294  	good = "::1"
   295  	verr = String(&good).IsIP()()
   296  	assert.Nil(verr)
   297  
   298  	bad := ""
   299  	verr = String(&bad).IsIP()()
   300  	assert.NotNil(verr)
   301  	assert.Equal(ErrStringIsIP, ErrCause(verr))
   302  
   303  	bad = "this is a test"
   304  	verr = String(&bad).IsIP()()
   305  	assert.NotNil(verr)
   306  	assert.Equal(ErrStringIsIP, ErrCause(verr))
   307  }
   308  
   309  func TestStringIsSlug(t *testing.T) {
   310  	assert := assert.New(t)
   311  
   312  	var verr error
   313  	verr = String(nil).IsSlug()()
   314  	assert.NotNil(verr)
   315  	assert.Nil(ErrValue(verr))
   316  	assert.Equal(ErrStringIsSlug, ErrCause(verr))
   317  
   318  	good := "abcdefghijklmnopqrstuvwxyz"
   319  	verr = String(&good).IsSlug()()
   320  	assert.Nil(verr)
   321  
   322  	good = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
   323  	verr = String(&good).IsSlug()()
   324  	assert.Nil(verr)
   325  
   326  	good = "0123456789"
   327  	verr = String(&good).IsSlug()()
   328  	assert.Nil(verr)
   329  
   330  	good = "_-"
   331  	verr = String(&good).IsSlug()()
   332  	assert.Nil(verr)
   333  
   334  	good = "shortcut_service"
   335  	verr = String(&good).IsSlug()()
   336  	assert.Nil(verr)
   337  
   338  	good = "Shortcut-Service"
   339  	verr = String(&good).IsSlug()()
   340  	assert.Nil(verr)
   341  
   342  	bad := "this/../is/../hacking?"
   343  	verr = String(&bad).IsSlug()()
   344  	assert.NotNil(verr)
   345  	assert.Equal(ErrStringIsSlug, ErrCause(verr))
   346  }
   347  
   348  func TestStringIsOneOf(t *testing.T) {
   349  	assert := assert.New(t)
   350  
   351  	var verr error
   352  	verr = String(nil).IsOneOf()()
   353  	assert.NotNil(verr)
   354  	assert.Equal(ErrStringIsOneOf, ErrCause(verr))
   355  	assert.Nil(ErrValue(verr))
   356  	assert.NotNil(ErrMessage(verr))
   357  
   358  	good := "foo"
   359  	verr = String(&good).IsOneOf("foo", "bar")()
   360  	assert.Nil(verr)
   361  
   362  	bad := "bad"
   363  	verr = String(&bad).IsOneOf("foo", "bar")()
   364  	assert.NotNil(verr)
   365  	assert.NotNil(ErrValue(verr))
   366  	assert.Equal(ErrStringIsOneOf, ErrCause(verr))
   367  	assert.Equal("foo, bar", ErrMessage(verr))
   368  }