mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
feat!: Validate monotonic numbers for rich parameters (#6046)
* Database changes * protobuf * Fix: docs * workspaces_test * Validation in coderd * Fix: resources * omitempty * UI changes * UI tests * fix
This commit is contained in:
@@ -2573,18 +2573,19 @@ func (q *fakeQuerier) InsertTemplateVersionParameter(_ context.Context, arg data
|
||||
|
||||
//nolint:gosimple
|
||||
param := database.TemplateVersionParameter{
|
||||
TemplateVersionID: arg.TemplateVersionID,
|
||||
Name: arg.Name,
|
||||
Description: arg.Description,
|
||||
Type: arg.Type,
|
||||
Mutable: arg.Mutable,
|
||||
DefaultValue: arg.DefaultValue,
|
||||
Icon: arg.Icon,
|
||||
Options: arg.Options,
|
||||
ValidationError: arg.ValidationError,
|
||||
ValidationRegex: arg.ValidationRegex,
|
||||
ValidationMin: arg.ValidationMin,
|
||||
ValidationMax: arg.ValidationMax,
|
||||
TemplateVersionID: arg.TemplateVersionID,
|
||||
Name: arg.Name,
|
||||
Description: arg.Description,
|
||||
Type: arg.Type,
|
||||
Mutable: arg.Mutable,
|
||||
DefaultValue: arg.DefaultValue,
|
||||
Icon: arg.Icon,
|
||||
Options: arg.Options,
|
||||
ValidationError: arg.ValidationError,
|
||||
ValidationRegex: arg.ValidationRegex,
|
||||
ValidationMin: arg.ValidationMin,
|
||||
ValidationMax: arg.ValidationMax,
|
||||
ValidationMonotonic: arg.ValidationMonotonic,
|
||||
}
|
||||
q.templateVersionParameters = append(q.templateVersionParameters, param)
|
||||
return param, nil
|
||||
|
||||
Generated
+5
-1
@@ -353,7 +353,9 @@ CREATE TABLE template_version_parameters (
|
||||
validation_regex text NOT NULL,
|
||||
validation_min integer NOT NULL,
|
||||
validation_max integer NOT NULL,
|
||||
validation_error text DEFAULT ''::text NOT NULL
|
||||
validation_error text DEFAULT ''::text NOT NULL,
|
||||
validation_monotonic text DEFAULT ''::text NOT NULL,
|
||||
CONSTRAINT validation_monotonic_order CHECK ((validation_monotonic = ANY (ARRAY['increasing'::text, 'decreasing'::text, ''::text])))
|
||||
);
|
||||
|
||||
COMMENT ON COLUMN template_version_parameters.name IS 'Parameter name';
|
||||
@@ -378,6 +380,8 @@ COMMENT ON COLUMN template_version_parameters.validation_max IS 'Validation: max
|
||||
|
||||
COMMENT ON COLUMN template_version_parameters.validation_error IS 'Validation: error displayed when the regex does not match.';
|
||||
|
||||
COMMENT ON COLUMN template_version_parameters.validation_monotonic IS 'Validation: consecutive values preserve the monotonic order';
|
||||
|
||||
CREATE TABLE template_versions (
|
||||
id uuid NOT NULL,
|
||||
template_id uuid,
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
ALTER TABLE template_version_parameters DROP CONSTRAINT validation_monotonic_order;
|
||||
|
||||
ALTER TABLE template_version_parameters DROP COLUMN validation_monotonic;
|
||||
@@ -0,0 +1,6 @@
|
||||
ALTER TABLE template_version_parameters ADD COLUMN validation_monotonic text NOT NULL DEFAULT '';
|
||||
|
||||
ALTER TABLE template_version_parameters ADD CONSTRAINT validation_monotonic_order CHECK (validation_monotonic IN ('increasing', 'decreasing', ''));
|
||||
|
||||
COMMENT ON COLUMN template_version_parameters.validation_monotonic
|
||||
IS 'Validation: consecutive values preserve the monotonic order';
|
||||
@@ -1456,6 +1456,8 @@ type TemplateVersionParameter struct {
|
||||
ValidationMax int32 `db:"validation_max" json:"validation_max"`
|
||||
// Validation: error displayed when the regex does not match.
|
||||
ValidationError string `db:"validation_error" json:"validation_error"`
|
||||
// Validation: consecutive values preserve the monotonic order
|
||||
ValidationMonotonic string `db:"validation_monotonic" json:"validation_monotonic"`
|
||||
}
|
||||
|
||||
type User struct {
|
||||
|
||||
@@ -3519,7 +3519,7 @@ func (q *sqlQuerier) UpdateTemplateMetaByID(ctx context.Context, arg UpdateTempl
|
||||
}
|
||||
|
||||
const getTemplateVersionParameters = `-- name: GetTemplateVersionParameters :many
|
||||
SELECT template_version_id, name, description, type, mutable, default_value, icon, options, validation_regex, validation_min, validation_max, validation_error FROM template_version_parameters WHERE template_version_id = $1
|
||||
SELECT template_version_id, name, description, type, mutable, default_value, icon, options, validation_regex, validation_min, validation_max, validation_error, validation_monotonic FROM template_version_parameters WHERE template_version_id = $1
|
||||
`
|
||||
|
||||
func (q *sqlQuerier) GetTemplateVersionParameters(ctx context.Context, templateVersionID uuid.UUID) ([]TemplateVersionParameter, error) {
|
||||
@@ -3544,6 +3544,7 @@ func (q *sqlQuerier) GetTemplateVersionParameters(ctx context.Context, templateV
|
||||
&i.ValidationMin,
|
||||
&i.ValidationMax,
|
||||
&i.ValidationError,
|
||||
&i.ValidationMonotonic,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -3572,7 +3573,8 @@ INSERT INTO
|
||||
validation_regex,
|
||||
validation_min,
|
||||
validation_max,
|
||||
validation_error
|
||||
validation_error,
|
||||
validation_monotonic
|
||||
)
|
||||
VALUES
|
||||
(
|
||||
@@ -3587,23 +3589,25 @@ VALUES
|
||||
$9,
|
||||
$10,
|
||||
$11,
|
||||
$12
|
||||
) RETURNING template_version_id, name, description, type, mutable, default_value, icon, options, validation_regex, validation_min, validation_max, validation_error
|
||||
$12,
|
||||
$13
|
||||
) RETURNING template_version_id, name, description, type, mutable, default_value, icon, options, validation_regex, validation_min, validation_max, validation_error, validation_monotonic
|
||||
`
|
||||
|
||||
type InsertTemplateVersionParameterParams struct {
|
||||
TemplateVersionID uuid.UUID `db:"template_version_id" json:"template_version_id"`
|
||||
Name string `db:"name" json:"name"`
|
||||
Description string `db:"description" json:"description"`
|
||||
Type string `db:"type" json:"type"`
|
||||
Mutable bool `db:"mutable" json:"mutable"`
|
||||
DefaultValue string `db:"default_value" json:"default_value"`
|
||||
Icon string `db:"icon" json:"icon"`
|
||||
Options json.RawMessage `db:"options" json:"options"`
|
||||
ValidationRegex string `db:"validation_regex" json:"validation_regex"`
|
||||
ValidationMin int32 `db:"validation_min" json:"validation_min"`
|
||||
ValidationMax int32 `db:"validation_max" json:"validation_max"`
|
||||
ValidationError string `db:"validation_error" json:"validation_error"`
|
||||
TemplateVersionID uuid.UUID `db:"template_version_id" json:"template_version_id"`
|
||||
Name string `db:"name" json:"name"`
|
||||
Description string `db:"description" json:"description"`
|
||||
Type string `db:"type" json:"type"`
|
||||
Mutable bool `db:"mutable" json:"mutable"`
|
||||
DefaultValue string `db:"default_value" json:"default_value"`
|
||||
Icon string `db:"icon" json:"icon"`
|
||||
Options json.RawMessage `db:"options" json:"options"`
|
||||
ValidationRegex string `db:"validation_regex" json:"validation_regex"`
|
||||
ValidationMin int32 `db:"validation_min" json:"validation_min"`
|
||||
ValidationMax int32 `db:"validation_max" json:"validation_max"`
|
||||
ValidationError string `db:"validation_error" json:"validation_error"`
|
||||
ValidationMonotonic string `db:"validation_monotonic" json:"validation_monotonic"`
|
||||
}
|
||||
|
||||
func (q *sqlQuerier) InsertTemplateVersionParameter(ctx context.Context, arg InsertTemplateVersionParameterParams) (TemplateVersionParameter, error) {
|
||||
@@ -3620,6 +3624,7 @@ func (q *sqlQuerier) InsertTemplateVersionParameter(ctx context.Context, arg Ins
|
||||
arg.ValidationMin,
|
||||
arg.ValidationMax,
|
||||
arg.ValidationError,
|
||||
arg.ValidationMonotonic,
|
||||
)
|
||||
var i TemplateVersionParameter
|
||||
err := row.Scan(
|
||||
@@ -3635,6 +3640,7 @@ func (q *sqlQuerier) InsertTemplateVersionParameter(ctx context.Context, arg Ins
|
||||
&i.ValidationMin,
|
||||
&i.ValidationMax,
|
||||
&i.ValidationError,
|
||||
&i.ValidationMonotonic,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
@@ -12,7 +12,8 @@ INSERT INTO
|
||||
validation_regex,
|
||||
validation_min,
|
||||
validation_max,
|
||||
validation_error
|
||||
validation_error,
|
||||
validation_monotonic
|
||||
)
|
||||
VALUES
|
||||
(
|
||||
@@ -27,7 +28,8 @@ VALUES
|
||||
$9,
|
||||
$10,
|
||||
$11,
|
||||
$12
|
||||
$12,
|
||||
$13
|
||||
) RETURNING *;
|
||||
|
||||
-- name: GetTemplateVersionParameters :many
|
||||
|
||||
Reference in New Issue
Block a user