-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Config UI: add param value validation (host) #26149
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
naltatis
wants to merge
8
commits into
master
Choose a base branch
from
feat/param_validate
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a8a8440
Template: add input validation (host)
naltatis 839c497
fix e2e
naltatis 723e5bd
Merge branch 'master' into feat/param_validate
naltatis 0cd7900
align pattern validation with required; better browser errors: add pa…
naltatis f562a70
fmt
naltatis 79156ea
review feedback
naltatis 556dbcf
Merge branch 'master' into feat/param_validate
naltatis c374546
cleanup
naltatis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| import { test, expect } from "@playwright/test"; | ||
| import { start, stop, baseUrl } from "./evcc"; | ||
| import { expectModalVisible } from "./utils"; | ||
|
|
||
| test.use({ baseURL: baseUrl() }); | ||
|
|
||
| test.describe("host pattern validation", async () => { | ||
| test.beforeEach(async () => { | ||
| await start(); | ||
| }); | ||
| test.afterEach(async () => { | ||
| await stop(); | ||
| }); | ||
|
|
||
| test("reject URL with scheme in host field", async ({ page }) => { | ||
| await page.goto("/#/config"); | ||
|
|
||
| const modal = page.getByTestId("meter-modal"); | ||
| await page.getByRole("button", { name: "Add solar or battery" }).click(); | ||
| await expectModalVisible(modal); | ||
| await modal.getByRole("button", { name: "Add solar meter" }).click(); | ||
|
|
||
| await modal.getByLabel("Title").fill("Test PV"); | ||
| await page.waitForLoadState("networkidle"); | ||
| await modal.getByLabel("Manufacturer").selectOption("APsystems EZ1"); | ||
|
|
||
| const hostInput = modal.getByLabel("IP address or hostname"); | ||
| await hostInput.fill("http://192.168.1.100"); | ||
|
|
||
| // Check browser invalid state | ||
| const isValid = await hostInput.evaluate((el: HTMLInputElement) => el.checkValidity()); | ||
| expect(isValid).toBe(false); | ||
|
|
||
| // Check validate status is still unknown (hasn't tried to validate yet) | ||
| const testResult = modal.getByTestId("test-result"); | ||
| await expect(testResult).toContainText("Status: unknown"); | ||
|
|
||
| // Manually delete the pattern attribute to bypass client validation | ||
| await hostInput.evaluate((el: HTMLInputElement) => el.removeAttribute("pattern")); | ||
| await testResult.getByRole("link", { name: "validate" }).click(); | ||
| await expect(testResult).toContainText("Status: failed"); | ||
| await expect(testResult).toContainText("does not match required pattern"); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,7 +3,9 @@ package templates | |
| import ( | ||
| "bytes" | ||
| _ "embed" | ||
| "errors" | ||
| "fmt" | ||
| "regexp" | ||
| "slices" | ||
| "strconv" | ||
| "strings" | ||
|
|
@@ -38,6 +40,27 @@ func (t *Template) UpdateParamsWithDefaults() error { | |
| return nil | ||
| } | ||
|
|
||
| // validatePattern checks if a value matches a pattern and returns a descriptive error if not | ||
| func validatePattern(regex, value string, examples []string) error { | ||
| if regex == "" { | ||
| return nil | ||
| } | ||
|
|
||
| matched, err := regexp.MatchString(regex, value) | ||
| if err != nil { | ||
| return fmt.Errorf("invalid regex pattern: %w", err) | ||
| } | ||
| if matched { | ||
| return nil | ||
| } | ||
|
|
||
| errMsg := fmt.Sprintf("value %q does not match required pattern", value) | ||
| if len(examples) > 0 { | ||
| errMsg += fmt.Sprintf(". Valid examples: %s", strings.Join(examples, ", ")) | ||
| } | ||
| return errors.New(errMsg) | ||
| } | ||
|
|
||
| // validate the template (only rudimentary for now) | ||
| func (t *Template) Validate() error { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wird Validate nur beim Speichern oder auch beim Config einlesen aufgerufen? |
||
| for _, c := range t.Capabilities { | ||
|
|
@@ -92,6 +115,15 @@ func (t *Template) Validate() error { | |
| } | ||
| } | ||
| } | ||
|
|
||
| // validate pattern examples against pattern | ||
| if p.Pattern.Regex != "" && len(p.Pattern.Examples) > 0 { | ||
| for _, example := range p.Pattern.Examples { | ||
| if err := validatePattern(p.Pattern.Regex, example, nil); err != nil { | ||
| return fmt.Errorf("param %s: pattern example %q is invalid: pattern=%q", p.Name, example, p.Pattern.Regex) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
|
|
@@ -362,6 +394,13 @@ func (t *Template) RenderResult(renderMode int, other map[string]any) ([]byte, m | |
| return nil, nil, fmt.Errorf("missing required `%s`", p.Name) | ||
| } | ||
|
|
||
| // validate pattern if defined | ||
| if s != "" && p.Pattern.Regex != "" { | ||
| if err := validatePattern(p.Pattern.Regex, s, p.Pattern.Examples); err != nil { | ||
| return nil, nil, fmt.Errorf("%s: %w", p.Name, err) | ||
| } | ||
| } | ||
|
|
||
| res[out] = s | ||
| } | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Die Methode gehört an den Param- spart die Parameter