schema.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. Copyright 2014 The Kubernetes Authors.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package validation
  14. import (
  15. "bytes"
  16. "encoding/json"
  17. "fmt"
  18. ejson "github.com/exponent-io/jsonpath"
  19. utilerrors "k8s.io/apimachinery/pkg/util/errors"
  20. )
  21. // Schema is an interface that knows how to validate an API object serialized to a byte array.
  22. type Schema interface {
  23. ValidateBytes(data []byte) error
  24. }
  25. // NullSchema always validates bytes.
  26. type NullSchema struct{}
  27. // ValidateBytes never fails for NullSchema.
  28. func (NullSchema) ValidateBytes(data []byte) error { return nil }
  29. // NoDoubleKeySchema is a schema that disallows double keys.
  30. type NoDoubleKeySchema struct{}
  31. // ValidateBytes validates bytes.
  32. func (NoDoubleKeySchema) ValidateBytes(data []byte) error {
  33. var list []error
  34. if err := validateNoDuplicateKeys(data, "metadata", "labels"); err != nil {
  35. list = append(list, err)
  36. }
  37. if err := validateNoDuplicateKeys(data, "metadata", "annotations"); err != nil {
  38. list = append(list, err)
  39. }
  40. return utilerrors.NewAggregate(list)
  41. }
  42. func validateNoDuplicateKeys(data []byte, path ...string) error {
  43. r := ejson.NewDecoder(bytes.NewReader(data))
  44. // This is Go being unfriendly. The 'path ...string' comes in as a
  45. // []string, and SeekTo takes ...interface{}, so we can't just pass
  46. // the path straight in, we have to copy it. *sigh*
  47. ifacePath := []interface{}{}
  48. for ix := range path {
  49. ifacePath = append(ifacePath, path[ix])
  50. }
  51. found, err := r.SeekTo(ifacePath...)
  52. if err != nil {
  53. return err
  54. }
  55. if !found {
  56. return nil
  57. }
  58. seen := map[string]bool{}
  59. for {
  60. tok, err := r.Token()
  61. if err != nil {
  62. return err
  63. }
  64. switch t := tok.(type) {
  65. case json.Delim:
  66. if t.String() == "}" {
  67. return nil
  68. }
  69. case ejson.KeyString:
  70. if seen[string(t)] {
  71. return fmt.Errorf("duplicate key: %s", string(t))
  72. }
  73. seen[string(t)] = true
  74. }
  75. }
  76. }
  77. // ConjunctiveSchema encapsulates a schema list.
  78. type ConjunctiveSchema []Schema
  79. // ValidateBytes validates bytes per a ConjunctiveSchema.
  80. func (c ConjunctiveSchema) ValidateBytes(data []byte) error {
  81. var list []error
  82. schemas := []Schema(c)
  83. for ix := range schemas {
  84. if err := schemas[ix].ValidateBytes(data); err != nil {
  85. list = append(list, err)
  86. }
  87. }
  88. return utilerrors.NewAggregate(list)
  89. }