Skip to content

Commit 3c8d2c9

Browse files
committed
Add parameter validation checks for length, allowed values, and number limits
1 parent 5d8d877 commit 3c8d2c9

File tree

2 files changed

+164
-0
lines changed

2 files changed

+164
-0
lines changed

dsc/tests/dsc_parameters.tests.ps1

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,4 +577,164 @@ Describe 'Parameters tests' {
577577
$LASTEXITCODE | Should -Be 4
578578
$testError | Should -Match 'Circular dependency or unresolvable parameter references detected in parameters'
579579
}
580+
581+
It 'Default value violates <constraint> constraint' -TestCases @(
582+
@{ constraint = 'minLength'; type = 'string'; value = 'ab'; min = 3; max = 20; errorMatch = 'minimum length' }
583+
@{ constraint = 'maxLength'; type = 'string'; value = 'verylongusernamethatexceedslimit'; min = 3; max = 20; errorMatch = 'maximum length' }
584+
@{ constraint = 'minValue'; type = 'int'; value = 0; min = 1; max = 65535; errorMatch = 'minimum value' }
585+
@{ constraint = 'maxValue'; type = 'int'; value = 99999; min = 1; max = 65535; errorMatch = 'maximum value' }
586+
) {
587+
param($constraint, $type, $value, $min, $max, $errorMatch)
588+
589+
if ($type -eq 'string') {
590+
$value = "'$value'"
591+
# For string type, only use length constraints
592+
$config_yaml = @"
593+
`$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
594+
parameters:
595+
param1:
596+
type: $type
597+
minLength: $min
598+
maxLength: $max
599+
defaultValue: $value
600+
resources:
601+
- name: Echo
602+
type: Microsoft.DSC.Debug/Echo
603+
properties:
604+
output: '[parameters(''param1'')]'
605+
"@
606+
} else {
607+
# For int type, only use value constraints
608+
$config_yaml = @"
609+
`$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
610+
parameters:
611+
param1:
612+
type: $type
613+
minValue: $min
614+
maxValue: $max
615+
defaultValue: $value
616+
resources:
617+
- name: Echo
618+
type: Microsoft.DSC.Debug/Echo
619+
properties:
620+
output: '[parameters(''param1'')]'
621+
"@
622+
}
623+
624+
$testError = & {$config_yaml | dsc config get -f - 2>&1}
625+
$LASTEXITCODE | Should -Be 4
626+
$testError | Should -Match $errorMatch
627+
}
628+
629+
It 'Default value violates allowedValues constraint for <type>' -TestCases @(
630+
@{ type = 'string'; value = 'staging'; allowed = @('dev', 'test', 'prod') }
631+
@{ type = 'int'; value = 7; allowed = @(1, 5, 10) }
632+
) {
633+
param($type, $value, $allowed)
634+
635+
if ($type -eq 'string') {
636+
$value = "'$value'"
637+
}
638+
639+
$config_yaml = @"
640+
`$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
641+
parameters:
642+
param1:
643+
type: $type
644+
allowedValues: $($allowed | ConvertTo-Json -Compress)
645+
defaultValue: $value
646+
resources:
647+
- name: Echo
648+
type: Microsoft.DSC.Debug/Echo
649+
properties:
650+
output: '[parameters(''param1'')]'
651+
"@
652+
653+
$testError = & {$config_yaml | dsc config get -f - 2>&1}
654+
$LASTEXITCODE | Should -Be 4
655+
$testError | Should -Match 'allowed values'
656+
}
657+
658+
It 'Default values pass constraint validation for <type>' -TestCases @(
659+
@{ type = 'string'; value = 'admin'; min = 3; max = 20 }
660+
@{ type = 'int'; value = 8080; min = 1; max = 65535 }
661+
) {
662+
param($type, $value, $min, $max)
663+
664+
if ($type -eq 'string') {
665+
$value = "'$value'"
666+
# For string type, only use length constraints
667+
$config_yaml = @"
668+
`$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
669+
parameters:
670+
param1:
671+
type: $type
672+
minLength: $min
673+
maxLength: $max
674+
defaultValue: $value
675+
resources:
676+
- name: Echo
677+
type: Microsoft.DSC.Debug/Echo
678+
properties:
679+
output: '[parameters(''param1'')]'
680+
"@
681+
} else {
682+
# For int type, only use value constraints
683+
$config_yaml = @"
684+
`$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
685+
parameters:
686+
param1:
687+
type: $type
688+
minValue: $min
689+
maxValue: $max
690+
defaultValue: $value
691+
resources:
692+
- name: Echo
693+
type: Microsoft.DSC.Debug/Echo
694+
properties:
695+
output: '[parameters(''param1'')]'
696+
"@
697+
}
698+
699+
$out = $config_yaml | dsc config get -f - | ConvertFrom-Json
700+
$LASTEXITCODE | Should -Be 0
701+
if ($type -eq 'string') {
702+
$out.results[0].result.actualState.output | Should -BeExactly 'admin'
703+
} else {
704+
$out.results[0].result.actualState.output | Should -BeExactly 8080
705+
}
706+
}
707+
708+
It 'Default values with allowedValues pass validation for <type>' -TestCases @(
709+
@{ type = 'string'; value = 'dev'; allowed = @('dev', 'test', 'prod') }
710+
@{ type = 'int'; value = 5; allowed = @(1, 5, 10) }
711+
) {
712+
param($type, $value, $allowed)
713+
714+
if ($type -eq 'string') {
715+
$value = "'$value'"
716+
}
717+
718+
$config_yaml = @"
719+
`$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
720+
parameters:
721+
param1:
722+
type: $type
723+
allowedValues: $($allowed | ConvertTo-Json -Compress)
724+
defaultValue: $value
725+
resources:
726+
- name: Echo
727+
type: Microsoft.DSC.Debug/Echo
728+
properties:
729+
output: '[parameters(''param1'')]'
730+
"@
731+
732+
$out = $config_yaml | dsc config get -f - | ConvertFrom-Json
733+
$LASTEXITCODE | Should -Be 0
734+
if ($type -eq 'string') {
735+
$out.results[0].result.actualState.output | Should -BeExactly 'dev'
736+
} else {
737+
$out.results[0].result.actualState.output | Should -BeExactly 5
738+
}
739+
}
580740
}

lib/dsc-lib/src/configure/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -895,6 +895,10 @@ impl Configurator {
895895
};
896896

897897
if let Ok(value) = value_result {
898+
check_length(name, &value, parameter)?;
899+
check_allowed_values(name, &value, parameter)?;
900+
check_number_limits(name, &value, parameter)?;
901+
898902
validate_parameter_type(name, &value, &parameter.parameter_type)?;
899903
self.context.parameters.insert(name.to_string(), (value, parameter.parameter_type.clone()));
900904
resolved_in_this_pass.push(name.clone());

0 commit comments

Comments
 (0)