...

Source file src/github.com/go-openapi/validate/schema_props_test.go

Documentation: github.com/go-openapi/validate

     1  // Copyright 2015 go-swagger maintainers
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //    http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package validate
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/go-openapi/spec"
    21  	"github.com/go-openapi/strfmt"
    22  	"github.com/stretchr/testify/assert"
    23  	"github.com/stretchr/testify/require"
    24  )
    25  
    26  // Test edge cases in schema_props_validator which are difficult
    27  // to simulate with specs
    28  // (this one is a trivial, just to check all methods are filled)
    29  func TestSchemaPropsValidator_EdgeCases(t *testing.T) {
    30  	t.Run("should validate props against empty validator", func(t *testing.T) {
    31  		s := newSchemaPropsValidator(
    32  			"", "", nil, nil, nil, nil, nil, nil, strfmt.Default, nil)
    33  		s.SetPath("path")
    34  		assert.Equal(t, "path", s.Path)
    35  	})
    36  
    37  	t.Run("with allOf", func(t *testing.T) {
    38  		makeValidator := func() EntityValidator {
    39  			return newSchemaPropsValidator(
    40  				"path", "body",
    41  				[]spec.Schema{
    42  					*spec.StringProperty(),
    43  					*spec.StrFmtProperty("date"),
    44  				}, nil, nil, nil, nil, nil, strfmt.Default, &SchemaValidatorOptions{recycleValidators: true})
    45  		}
    46  
    47  		t.Run("should validate date string", func(t *testing.T) {
    48  			s := makeValidator()
    49  
    50  			const data = "2024-01-25"
    51  			res := s.Validate(data)
    52  			require.NotNil(t, res)
    53  			require.Empty(t, res.Errors)
    54  		})
    55  
    56  		t.Run("should NOT validate unformatted string", func(t *testing.T) {
    57  			s := makeValidator()
    58  
    59  			const data = "string_value"
    60  			res := s.Validate(data)
    61  			require.NotNil(t, res)
    62  			require.NotEmpty(t, res.Errors)
    63  		})
    64  
    65  		t.Run("should NOT validate number", func(t *testing.T) {
    66  			s := makeValidator()
    67  
    68  			const data = 1
    69  			res := s.Validate(data)
    70  			require.NotNil(t, res)
    71  			require.NotEmpty(t, res.Errors)
    72  		})
    73  	})
    74  
    75  	t.Run("with oneOf", func(t *testing.T) {
    76  		makeValidator := func() EntityValidator {
    77  			return newSchemaPropsValidator(
    78  				"path", "body",
    79  				nil,
    80  				[]spec.Schema{
    81  					*spec.Int64Property(),
    82  					*spec.StrFmtProperty("date"),
    83  				}, nil, nil, nil, nil, strfmt.Default, &SchemaValidatorOptions{recycleValidators: true})
    84  		}
    85  
    86  		t.Run("should validate date string", func(t *testing.T) {
    87  			s := makeValidator()
    88  
    89  			const data = "2024-01-01"
    90  			res := s.Validate(data)
    91  			require.NotNil(t, res)
    92  			require.Empty(t, res.Errors)
    93  		})
    94  
    95  		t.Run("should validate number", func(t *testing.T) {
    96  			s := makeValidator()
    97  
    98  			const data = 1
    99  			res := s.Validate(data)
   100  			require.NotNil(t, res)
   101  			require.Empty(t, res.Errors)
   102  		})
   103  	})
   104  
   105  	t.Run("with anyOf", func(t *testing.T) {
   106  		makeValidator := func() EntityValidator {
   107  			return newSchemaPropsValidator(
   108  				"path", "body",
   109  				nil,
   110  				nil,
   111  				[]spec.Schema{
   112  					*spec.StringProperty(),
   113  					*spec.StrFmtProperty("date"),
   114  				}, nil, nil, nil, strfmt.Default, &SchemaValidatorOptions{recycleValidators: true})
   115  		}
   116  
   117  		t.Run("should validate date string", func(t *testing.T) {
   118  			s := makeValidator()
   119  
   120  			const data = "2024-01-01"
   121  			res := s.Validate(data)
   122  			require.NotNil(t, res)
   123  			require.Empty(t, res.Errors)
   124  		})
   125  
   126  		t.Run("should validate unformatted string", func(t *testing.T) {
   127  			s := makeValidator()
   128  
   129  			const data = "string_value"
   130  			res := s.Validate(data)
   131  			require.NotNil(t, res)
   132  			require.Empty(t, res.Errors)
   133  		})
   134  	})
   135  
   136  	t.Run("with not", func(t *testing.T) {
   137  		makeValidator := func() EntityValidator {
   138  			return newSchemaPropsValidator(
   139  				"path", "body",
   140  				nil,
   141  				nil,
   142  				nil,
   143  				spec.StringProperty(),
   144  				nil, nil, strfmt.Default, &SchemaValidatorOptions{recycleValidators: true})
   145  		}
   146  
   147  		t.Run("should validate number", func(t *testing.T) {
   148  			s := makeValidator()
   149  
   150  			const data = 1
   151  			res := s.Validate(data)
   152  			require.NotNil(t, res)
   153  			require.Empty(t, res.Errors)
   154  		})
   155  
   156  		t.Run("should NOT validate string", func(t *testing.T) {
   157  			s := makeValidator()
   158  
   159  			const data = "string_value"
   160  			res := s.Validate(data)
   161  			require.NotNil(t, res)
   162  			require.NotEmpty(t, res.Errors)
   163  		})
   164  	})
   165  
   166  	t.Run("with nested schema props", func(t *testing.T) {
   167  		makeValidator := func() EntityValidator {
   168  			return newSchemaValidator(
   169  				&spec.Schema{
   170  					SchemaProps: spec.SchemaProps{
   171  						AllOf: []spec.Schema{
   172  							{
   173  								SchemaProps: spec.SchemaProps{
   174  									OneOf: []spec.Schema{
   175  										{
   176  											SchemaProps: spec.SchemaProps{
   177  												AnyOf: []spec.Schema{
   178  													{
   179  														SchemaProps: spec.SchemaProps{
   180  															Not: spec.StringProperty(),
   181  														},
   182  													},
   183  													*spec.BoolProperty(),
   184  												},
   185  											},
   186  										},
   187  										*spec.StringProperty(),
   188  									},
   189  								},
   190  							},
   191  							*spec.Int64Property(),
   192  						},
   193  					},
   194  				},
   195  				nil,
   196  				"root",
   197  				strfmt.Default, &SchemaValidatorOptions{recycleValidators: true})
   198  		}
   199  
   200  		t.Run("should validate number", func(t *testing.T) {
   201  			s := makeValidator()
   202  
   203  			const data = 1
   204  			res := s.Validate(data)
   205  			require.NotNil(t, res)
   206  			require.Empty(t, res.Errors)
   207  		})
   208  
   209  		t.Run("should NOT validate string", func(t *testing.T) {
   210  			s := makeValidator()
   211  
   212  			const data = "string_value"
   213  			res := s.Validate(data)
   214  			require.NotNil(t, res)
   215  			require.NotEmpty(t, res.Errors)
   216  		})
   217  
   218  		t.Run("should exit early and redeem children validator", func(t *testing.T) {
   219  			s := makeValidator()
   220  
   221  			res := s.Validate(nil)
   222  			require.NotNil(t, res)
   223  			require.Empty(t, res.Errors)
   224  		})
   225  	})
   226  }
   227  

View as plain text