Skip to content

Conversation

@alexchristy
Copy link
Member

@alexchristy alexchristy commented Jul 26, 2025

Checklist

  • I have added a version label to my PR (e.g., patch, minor, major).
  • I have tested my changes locally and verified they work as expected.
  • I have added relevant tests to cover my changes.
  • I have made any necessary updates to the documentation.
  • I have made any necessary updates to the CLI.
  • I have made any necessary updates to the frontend.

Description

This PR simplifies validation logic for the Range, VPC, Subnet, and Host schemas by leveraging Pydantic's model_validator that provides access to class attributes after each field has been individually validated. This allows for the removal of all the if not field checks.

Additionally, this PR deduplicates the validation logic by centralizing the validation functions in a ValidationSchema class that can be inherited

Fixes: #107

@alexchristy alexchristy added minor Increment the minor version when merged backend Related to the OpenLabs backend/API labels Jul 26, 2025
@alexchristy alexchristy marked this pull request as ready for review July 26, 2025 12:55
Copilot AI review requested due to automatic review settings July 26, 2025 12:55
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR simplifies schema validation logic by migrating from Pydantic's field_validator to model_validator with mode "after", enabling access to all validated class attributes and eliminating redundant null checks. The change centralizes validation logic through reusable mixin classes that can be inherited across different schema types.

Key changes:

  • Replaced individual field validators with model validators that access validated class attributes directly
  • Created validation mixin classes (VPCCreateValidationMixin, SubnetCreateValidationMixin, RangeCreateValidationMixin) to eliminate code duplication
  • Removed null/existence checks for fields that are guaranteed to be validated by Pydantic

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 5 comments.

File Description
vpc_schemas.py Introduces VPCCreateValidationMixin with model validators and removes duplicate validation logic from both blueprint and deployed VPC creation schemas
subnet_schemas.py Creates SubnetCreateValidationMixin with model validators and refactors blueprint and deployed subnet schemas to use the mixin
range_schemas.py Adds RangeCreateValidationMixin with model validators for VPC validation and removes duplicate validation code from range creation schemas
host_schemas.py Converts field validator to model validator for size validation, removing the need for null checks on the OS field

raise ValueError(msg)

msg = f"All subnet in VPC: {vpc_name} must have unique names."
msg = f"All subnet in VPC: {self.name} must have unique names."
Copy link

Copilot AI Jul 26, 2025

Choose a reason for hiding this comment

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

Grammar error: 'All subnet' should be 'All subnets' (missing 's' for plural).

Suggested change
msg = f"All subnet in VPC: {self.name} must have unique names."
msg = f"All subnets in VPC: {self.name} must have unique names."

Copilot uses AI. Check for mistakes.

vpc_cidrs = [vpc.cidr for vpc in self.vpcs]
if not mutually_exclusive_networks_v4(vpc_cidrs):
msg = "All VPCs in the range must be mutually exclusive (not overlap)."
Copy link

Copilot AI Jul 26, 2025

Choose a reason for hiding this comment

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

[nitpick] The error message differs from the original 'All VPCs in range should be mutually exclusive (not overlap).' Consider maintaining consistency with the original wording unless the change is intentional.

Suggested change
msg = "All VPCs in the range must be mutually exclusive (not overlap)."
msg = "All VPCs in range should be mutually exclusive (not overlap)."

Copilot uses AI. Check for mistakes.
Copy link
Member

@Nareshp1 Nareshp1 left a comment

Choose a reason for hiding this comment

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

LGTM

@Adamkadaban Adamkadaban mentioned this pull request Dec 31, 2025
7 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backend Related to the OpenLabs backend/API minor Increment the minor version when merged

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Change from Field to Model Validators in Blueprint and Deployed Schemas

3 participants