all lists on lists.proxmox.com
 help / color / mirror / Atom feed
From: Hannes Laimer <h.laimer@proxmox.com>
To: pdm-devel@lists.proxmox.com
Subject: [pdm-devel] [PATCH 1/2] schema2rust: extract pattern validation from nested API schema fields
Date: Wed,  1 Oct 2025 17:47:16 +0200	[thread overview]
Message-ID: <20251001154717.405839-2-h.laimer@proxmox.com> (raw)
In-Reply-To: <20251001154717.405839-1-h.laimer@proxmox.com>

Previously, pattern validation was only extracted from top-level string fields,
leaving nested pattern fields and array item patterns unprocessed. This change
enables pattern extraction from nested schema structures, ensuring regex
validation is generated for all pattern fields regardless of their depth.
The generated Rust code now includes const_regex! definitions and
ApiStringFormat::Pattern references for previously ignored pattern fields.

Signed-off-by: Hannes Laimer <h.laimer@proxmox.com>
---
 pve-api-types/generator-lib/Schema2Rust.pm | 27 +++++++++++++++++++---
 1 file changed, 24 insertions(+), 3 deletions(-)

diff --git a/pve-api-types/generator-lib/Schema2Rust.pm b/pve-api-types/generator-lib/Schema2Rust.pm
index 0f3efc0a..794c7abe 100644
--- a/pve-api-types/generator-lib/Schema2Rust.pm
+++ b/pve-api-types/generator-lib/Schema2Rust.pm
@@ -206,6 +206,25 @@ my sub print_struct : prototype($$$) {
 
     my @arrays;
 
+    # Collect array fields
+    for my $field (sort keys $def->{fields}->%*) {
+        my $field_def = $def->{fields}->{$field};
+        if ($field_def->{kind} eq 'array-field') {
+            push @arrays, $field_def;
+        }
+    }
+
+    # Add regex constants from array fields to the main struct's API object
+    for my $array (@arrays) {
+        my $type_name = $array->{array_type_name};
+        next if $done_array_types->{$type_name};
+        if ($API && $array->{api}->{-regexes}) {
+            for my $name (keys %{$array->{api}->{-regexes}}) {
+                $def->{api}->{-regexes}->{$name} = $array->{api}->{-regexes}->{$name};
+            }
+        }
+    }
+
     print_api_string($out, $def->{api}, 'struct', $def->{name});
 
     if (length($def->{description})) {
@@ -217,9 +236,6 @@ my sub print_struct : prototype($$$) {
     print {$out} "pub struct $def->{name} {\n";
     for my $field (sort keys $def->{fields}->%*) {
         my $field_def = $def->{fields}->{$field};
-        if ($field_def->{kind} eq 'array-field') {
-            push @arrays, $field_def;
-        }
 
         my $name = $field_def->{name};
         my $rust_name = $field_def->{rust_name};
@@ -1097,6 +1113,11 @@ my sub string_type : prototype($$$$) {
         # if (my $kind = $fmt->{kind}) {
         #     $api_props->{format_fixme} = '"LIST TYPE"';
         # }
+    } elsif (defined(my $pattern = delete $schema->{pattern})) {
+        # Handle pattern field directly from schema if no format was specified
+        my $re_name = namify_const(${name_hint}, 're');
+        $api_props->{-regexes}->{$re_name} = $pattern;
+        $api_props->{format} = "&ApiStringFormat::Pattern(&$re_name)";
     }
 
     return 'String';
-- 
2.47.3



_______________________________________________
pdm-devel mailing list
pdm-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pdm-devel


  reply	other threads:[~2025-10-01 15:47 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-10-01 15:47 [pdm-devel] [PATCH proxmox 0/2] schema2rust: use 'pattern' if specified Hannes Laimer
2025-10-01 15:47 ` Hannes Laimer [this message]
2025-10-01 15:47 ` [pdm-devel] [PATCH 2/2] pve-api-types: regenerate Hannes Laimer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20251001154717.405839-2-h.laimer@proxmox.com \
    --to=h.laimer@proxmox.com \
    --cc=pdm-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal