github.com/m3db/m3@v1.5.0/src/query/api/v1/validators/validators.go (about) 1 // Copyright (c) 2020 Uber Technologies, Inc. 2 // 3 // Permission is hereby granted, free of charge, to any person obtaining a copy 4 // of this software and associated documentation files (the "Software"), to deal 5 // in the Software without restriction, including without limitation the rights 6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 // copies of the Software, and to permit persons to whom the Software is 8 // furnished to do so, subject to the following conditions: 9 // 10 // The above copyright notice and this permission notice shall be included in 11 // all copies or substantial portions of the Software. 12 // 13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 // THE SOFTWARE. 20 21 //package validators contains validation logics for the api. 22 package validators 23 24 import ( 25 "errors" 26 "fmt" 27 28 "github.com/m3db/m3/src/dbnode/namespace" 29 xerrors "github.com/m3db/m3/src/x/errors" 30 ) 31 32 var ( 33 // NamespaceValidator is an instance of namespaceValidator. 34 NamespaceValidator = &namespaceValidator{} 35 36 // ErrNamespaceExists is returned when trying to create a namespace with id that already exists. 37 ErrNamespaceExists = errors.New("namespace with the same ID already exists") 38 ) 39 40 type namespaceValidator struct{} 41 42 // Validate new namespace inputs only. Validation that applies to namespaces 43 // regardless of create/update/etc belongs in the option-specific Validate 44 // functions which are invoked on every change operation. 45 func (h *namespaceValidator) ValidateNewNamespace( 46 ns namespace.Metadata, 47 existing []namespace.Metadata, 48 ) error { 49 var ( 50 id = ns.ID() 51 indexBlockSize = ns.Options().RetentionOptions().BlockSize() 52 retentionBlockSize = ns.Options().IndexOptions().BlockSize() 53 ) 54 55 if indexBlockSize != retentionBlockSize { 56 return xerrors.NewInvalidParamsError( 57 fmt.Errorf("index and retention block size must match (%v, %v)", 58 indexBlockSize, 59 retentionBlockSize)) 60 } 61 62 for _, existingNs := range existing { 63 if id.Equal(existingNs.ID()) { 64 return ErrNamespaceExists 65 } 66 } 67 68 return nil 69 }