Skip to content

feat: add constructor validation checks to BasePolicy - #333855

Open
Deniz Güney Yıldırım (denizguney) wants to merge 2 commits into
microsoft:mainfrom
denizguney:patch-28
Open

feat: add constructor validation checks to BasePolicy#333855
Deniz Güney Yıldırım (denizguney) wants to merge 2 commits into
microsoft:mainfrom
denizguney:patch-28

Conversation

@denizguney

Copy link
Copy Markdown
Contributor

Closes #333852

Summary

  • Added input validation checks inside the BasePolicy constructor to ensure robust error handling.
  • Validates mandatory parameters (type, name, category, description, moduleName).
  • Added semantic versioning regex validation for minimumVersion.

Copilot AI balanced review requested due to automatic review settings September 1, 2026 20:03

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Empty module names now break every policy factory, and validation remains incomplete and insufficiently tested.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

Adds defensive constructor validation to BasePolicy.

Changes:

  • Validates required policy metadata.
  • Checks minimumVersion formatting.
  • Adds descriptive constructor errors.
File summaries
File Description
build/lib/policies/basePolicy.ts Adds constructor validation for policy fields.
Review details

Suppressed comments (4)

build/lib/policies/basePolicy.ts:41

  • This rejects every policy created by the existing factories: BooleanPolicy.from, NumberPolicy.from, ObjectPolicy.from, StringPolicy.from, and StringEnumPolicy.from all intentionally pass '' because translations are flattened into the empty-string module namespace. Consequently, parsing the checked-in policy catalog now throws before rendering. Keep the empty namespace valid while still rejecting non-string values.
		if (!moduleName || typeof moduleName !== 'string' || moduleName.trim() === '') {
			throw new Error("BasePolicy: Geçerli bir 'moduleName' değeri gereklidir.");

build/lib/policies/basePolicy.ts:35

  • This pattern accepts values such as "1" and "1.2.3.4", although PolicyDto.minimumVersion is defined as a two-component major.minor value and the renderer's supported-version identifiers follow that convention. Require exactly two numeric components so the new check actually rejects malformed policy versions.
		if (!minimumVersion || typeof minimumVersion !== 'string' || !/^\d+(\.\d+)*$/.test(minimumVersion)) {
			throw new Error(`BasePolicy: Geçersiz 'minimumVersion' formatı (${minimumVersion}). Sürüm numarası sayısal değerlerden oluşmalıdır (örn. '1.0.0').`);

build/lib/policies/basePolicy.ts:26

  • The newly thrown diagnostics are in Turkish while the surrounding build tooling reports errors in English. These messages will be exposed to all contributors running policy generation, so rewrite all of the new constructor errors in English to keep failures understandable and consistent.
			throw new Error("BasePolicy: 'type' parametresi zorunludur ve boş bırakılamaz.");

build/lib/policies/basePolicy.ts:25

  • None of the new validation branches has a targeted test. Add BasePolicy test coverage for each required field and version boundary, including the valid empty module namespace used by every concrete factory; this would catch both over-rejection and under-validation regressions.
		if (!type) {
  • Files reviewed: 1/1 changed files
  • Comments generated: 1
  • Review effort level: Balanced

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread build/lib/policies/basePolicy.ts Outdated
Comment on lines +25 to +32
if (!type) {
throw new Error("BasePolicy: 'type' parametresi zorunludur ve boş bırakılamaz.");
}
if (!name || typeof name !== 'string' || name.trim() === '') {
throw new Error("BasePolicy: Geçerli bir 'name' değeri gereklidir.");
}
if (!category) {
throw new Error("BasePolicy: 'category' parametresi zorunludur.");
Refine validation to support empty moduleName, enforce major.minor version format, use English error messages, and add structured type/category checks.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add validation checks to BasePolicy constructor for robust error handling

3 participants