Skip to content

Commit

Permalink
add isDeployments decorator
Browse files Browse the repository at this point in the history
Signed-off-by: Ivo Yankov <[email protected]>
  • Loading branch information
Ivo-Yankov committed Nov 22, 2024
1 parent 00c5800 commit 30ac6ad
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/core/config/local_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { MissingArgumentError, SoloError } from '../errors.js'
import { promptDeploymentClusters, promptDeploymentName, promptUserEmailAddress } from '../../commands/prompts.js'
import { type SoloLogger } from '../logging.js'
import { Task } from '../task.js'
import { IsDeployments } from '../validator_decorators.js'

export class LocalConfig implements LocalConfigData {
@IsNotEmpty()
Expand All @@ -34,6 +35,7 @@ export class LocalConfig implements LocalConfigData {
// so it needs to be available in all targeted clusters
@IsNotEmpty()
@IsObject()
@IsDeployments()
deployments: Deployments

@IsNotEmpty()
Expand Down
51 changes: 51 additions & 0 deletions src/core/validator_decorators.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* Copyright (C) 2024 Hedera Hashgraph, LLC
*
* Licensed under the Apache License, Version 2.0 (the ""License"");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an ""AS IS"" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
import { registerDecorator, type ValidationOptions, type ValidationArguments } from 'class-validator'

const isObject = (obj) => obj === Object(obj)

export const IsDeployments = (validationOptions?: ValidationOptions) => {
return function (object: any, propertyName: string) {
registerDecorator({
name: 'IsDeployments',
target: object.constructor,
propertyName: propertyName,
constraints: [],
options: {
message: 'Wrong deployments format',
...validationOptions,
},
validator: {
validate (value: any, args: ValidationArguments) {
if (!isObject(value)) return false
if (Object.keys(value).length === 0) return true

const keys = Object.keys(value)

return keys.every(key => {
if (typeof key !== 'string') return false
if (!isObject(value[key])) return false
if (!Array.isArray(value[key].clusterAliases)) return false
if (!value[key].clusterAliases.every(val => typeof val === 'string')) return false

return true
})
},
},
})
}
}
4 changes: 2 additions & 2 deletions test/unit/core/local_config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,12 @@ describe('LocalConfig', () => {
{ clusterAliases: [5, 6, 7] },
{ clusterAliases: 'bar' },
{ clusterAliases: 5 },
{ clusterAliases: { foo: 'bar '} }
{ clusterAliases: { foo: 'bar ' } }
]

for ( const invalidDeployment of invalidDeployments ) {
const deployments = {
'valid-deployment': validDeployment,
'my-deployment': validDeployment,
'invalid-deployment': invalidDeployment
}

Expand Down

0 comments on commit 30ac6ad

Please sign in to comment.