You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by "Revolyssup (via GitHub)" <gi...@apache.org> on 2025/10/30 14:50:48 UTC

[PR] feat: add validate API to standalone mode [apisix]

Revolyssup opened a new pull request, #12718:
URL: https://github.com/apache/apisix/pull/12718

   Provide a validate API for standalone mode that only validate input like admin api but does not save input to memory.
   Required for ingress controller to validate plugin configurations that convert from CRD.
   
   ### Checklist
   
   - [ ] I have explained the need for this PR and the problem it solves
   - [ ] I have explained the changes or the new features added to this PR
   - [ ] I have added tests corresponding to this change
   - [ ] I have updated the documentation to reflect this change
   - [ ] I have verified that this change is backward compatible (If not, please discuss on the [APISIX mailing list](https://github.com/apache/apisix/tree/master#community) first)
   
   <!--
   
   Note
   
   1. Mark the PR as draft until it's ready to be reviewed.
   2. Always add/update tests for any changes unless you have a good reason.
   3. Always update the documentation to reflect the changes made in the PR.
   4. Make a new commit to resolve conversations instead of `push -f`.
   5. To resolve merge conflicts, merge master instead of rebasing.
   6. Use "request review" to notify the reviewer after making changes.
   7. Only a reviewer can mark a conversation as resolved.
   
   -->
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat: add validate API to standalone mode [apisix]

Posted by "AlinsRan (via GitHub)" <gi...@apache.org>.
AlinsRan commented on PR #12718:
URL: https://github.com/apache/apisix/pull/12718#issuecomment-3551359104

   The CI failure is not related to the current PR modification.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat: add validate API to standalone mode [apisix]

Posted by "bzp2010 (via GitHub)" <gi...@apache.org>.
bzp2010 commented on code in PR #12718:
URL: https://github.com/apache/apisix/pull/12718#discussion_r2548436344


##########
t/admin/standalone.spec.ts:
##########
@@ -194,6 +194,86 @@ describe('Admin - Standalone', () => {
   });
 
   describe('Normal', () => {
+    it('validate config (success case with json)', async () => {
+      const resp = await client.post(`${ENDPOINT}/validate`, config1);
+      expect(resp.status).toEqual(200);
+      expect(resp.data).toEqual({
+        message: 'Configuration is valid',
+        valid: true,
+      });
+    });
+
+    it('validate config (success case with yaml)', async () => {
+      const resp = await client.post(`${ENDPOINT}/validate`, YAML.stringify(config1), {
+        headers: { 'Content-Type': 'application/yaml' },
+      });
+      expect(resp.status).toEqual(200);
+      expect(resp.data).toEqual({
+        message: 'Configuration is valid',
+        valid: true,
+      });
+    });
+
+    it('validate config (success case with multiple resources)', async () => {
+      const multiResourceConfig = {
+        routes: [
+          {
+            id: 'r1',
+            uri: '/r1',
+            upstream: {
+              nodes: { '127.0.0.1:1980': 1 },
+              type: 'roundrobin',
+            },
+          },
+          {
+            id: 'r2',
+            uri: '/r2',
+            upstream: {
+              nodes: { '127.0.0.1:1980': 1 },
+              type: 'roundrobin',
+            },
+          },
+        ],
+        services: [
+          {
+            id: 's1',
+            upstream: {
+              nodes: { '127.0.0.1:1980': 1 },
+              type: 'roundrobin',
+            },
+          },
+        ],
+        routes_conf_version: 1,
+        services_conf_version: 1,
+      };
+
+      const resp = await client.post(`${ENDPOINT}/validate`, multiResourceConfig);
+      expect(resp.status).toEqual(200);
+      expect(resp.data).toEqual({
+        message: 'Configuration is valid',
+        valid: true,
+      });
+    });
+
+    it('validate config with consumer credentials', async () => {
+      const resp = await client.post(`${ENDPOINT}/validate`, credential1);
+      expect(resp.status).toEqual(200);
+      expect(resp.data).toEqual({
+        message: 'Configuration is valid',
+        valid: true,
+      });
+    });
+
+    it('validate config does not persist changes', async () => {
+      // First validate a configuration
+      const validateResp = await client.post(`${ENDPOINT}/validate`, config1);
+      expect(validateResp.status).toEqual(200);
+
+      // Then check that the configuration was not persisted
+      const getResp = await client.get(ENDPOINT);
+      expect(getResp.data.routes).toBeUndefined();
+    });

Review Comment:
   I recommend moving these validate API tests to a separate describe block, which will help achieve a clearer separation.



##########
t/admin/standalone.spec.ts:
##########
@@ -641,5 +721,228 @@ describe('Admin - Standalone', () => {
           'invalid routes at index 0, err: invalid configuration: failed to match pattern "^((uri|server_name|server_addr|request_uri|remote_port|remote_addr|query_string|host|hostname|mqtt_client_id)|arg_[0-9a-zA-z_-]+)$" with "args_invalid"',
       });
     });
+    it('validate config (duplicate route id)', async () => {

Review Comment:
   What is the purpose of these newly added tests? Did we previously have duplicate ID checking functionality without test coverage?



##########
apisix/admin/standalone.lua:
##########
@@ -158,6 +157,120 @@ local function check_conf(checker, schema, item, typ)
 end
 
 
+local function validate_configuration(req_body, collect_all_errors)
+    local validation_results = {
+        valid = true,
+        errors = {}
+    }
+
+    for key, conf_version_key in pairs(ALL_RESOURCE_KEYS) do
+        local items = req_body[key]
+        local resource = resources[key] or {}
+
+        -- Validate conf_version_key if present
+        local new_conf_version = req_body[conf_version_key]
+        if new_conf_version and type(new_conf_version) ~= "number" then
+            if not collect_all_errors then
+                return false, conf_version_key .. " must be a number"
+            end
+            validation_results.valid = false
+            table_insert(validation_results.errors, {
+                resource_type = key,
+                error = conf_version_key .. " must be a number, got " .. type(new_conf_version)
+            })
+        end
+
+        if items and #items > 0 then
+            local item_schema = resource.schema
+            local item_checker = resource.checker
+            local id_set = {}
+
+            for index, item in ipairs(items) do
+                local item_temp = tbl_deepcopy(item)
+                local valid, err = check_conf(item_checker, item_schema, item_temp, key)
+                if not valid then
+                    local err_prefix = "invalid " .. key .. " at index " .. (index - 1) .. ", err: "
+                    local err_msg = type(err) == "table" and err.error_msg or err
+                    local error_msg = err_prefix .. err_msg
+
+                    if not collect_all_errors then
+                        return false, error_msg
+                    end
+                    validation_results.valid = false
+                    table_insert(validation_results.errors, {
+                        resource_type = key,
+                        index = index - 1,
+                        error = error_msg
+                    })
+                end
+
+                -- check for duplicate IDs
+                local duplicated, dup_err = check_duplicate(item, key, id_set)
+                if duplicated then
+                    if not collect_all_errors then
+                        return false, dup_err
+                    end
+                    validation_results.valid = false
+                    table_insert(validation_results.errors, {
+                        resource_type = key,
+                        index = index - 1,
+                        error = dup_err
+                    })
+                end
+            end
+        end
+    end
+
+    if collect_all_errors then
+        return validation_results.valid, validation_results
+    else
+        return validation_results.valid, nil
+    end
+end
+
+local function validate(ctx)
+    local content_type = core.request.header(nil, "content-type") or "application/json"
+    local req_body, err = core.request.get_body()
+    if err then
+        return core.response.exit(400, {error_msg = "invalid request body: " .. err})
+    end
+
+    if not req_body or #req_body <= 0 then
+        return core.response.exit(400, {error_msg = "invalid request body: empty request body"})
+    end
+
+    local data
+    if core.string.has_prefix(content_type, "application/yaml") then
+        local ok, result = pcall(yaml.load, req_body, { all = false })
+        if not ok or type(result) ~= "table" then
+            err = "invalid yaml request body"
+        else
+            data = result
+        end
+    else
+        data, err = core.json.decode(req_body)
+    end
+
+    if err then
+        core.log.error("invalid request body: ", req_body, " err: ", err)
+        return core.response.exit(400, {error_msg = "invalid request body: " .. err})
+    end
+
+    local valid, validation_results = validate_configuration(data, true)
+    if not valid then
+        return core.response.exit(400, {
+            error_msg = "Configuration validation failed",
+            valid = false,
+            errors = validation_results.errors
+        })
+    end
+
+    return core.response.exit(200, {
+        message = "Configuration is valid",
+        valid = true
+    })

Review Comment:
   When the check passes, neither the `message` nor the `valid` field carries any meaning; an HTTP 200 response is sufficient to imply this outcome. In practice, neither of these return fields will be utilised anywhere. **Remove them.**
   
   When the check fails, the `valid` field similarly holds no significance. An HTTP 400 response can indicate this error. **Remove it.** The `error_msg` should maintain a consistent style, **using lowercase initial letters.** 



##########
t/admin/standalone.spec.ts:
##########
@@ -194,6 +194,86 @@ describe('Admin - Standalone', () => {
   });
 
   describe('Normal', () => {
+    it('validate config (success case with json)', async () => {
+      const resp = await client.post(`${ENDPOINT}/validate`, config1);
+      expect(resp.status).toEqual(200);
+      expect(resp.data).toEqual({
+        message: 'Configuration is valid',
+        valid: true,
+      });
+    });
+
+    it('validate config (success case with yaml)', async () => {
+      const resp = await client.post(`${ENDPOINT}/validate`, YAML.stringify(config1), {
+        headers: { 'Content-Type': 'application/yaml' },
+      });
+      expect(resp.status).toEqual(200);
+      expect(resp.data).toEqual({
+        message: 'Configuration is valid',
+        valid: true,
+      });
+    });
+
+    it('validate config (success case with multiple resources)', async () => {
+      const multiResourceConfig = {
+        routes: [
+          {
+            id: 'r1',
+            uri: '/r1',
+            upstream: {
+              nodes: { '127.0.0.1:1980': 1 },
+              type: 'roundrobin',
+            },
+          },
+          {
+            id: 'r2',
+            uri: '/r2',
+            upstream: {
+              nodes: { '127.0.0.1:1980': 1 },
+              type: 'roundrobin',
+            },
+          },
+        ],
+        services: [
+          {
+            id: 's1',
+            upstream: {
+              nodes: { '127.0.0.1:1980': 1 },
+              type: 'roundrobin',
+            },
+          },
+        ],
+        routes_conf_version: 1,
+        services_conf_version: 1,
+      };
+
+      const resp = await client.post(`${ENDPOINT}/validate`, multiResourceConfig);
+      expect(resp.status).toEqual(200);
+      expect(resp.data).toEqual({
+        message: 'Configuration is valid',
+        valid: true,
+      });
+    });
+
+    it('validate config with consumer credentials', async () => {
+      const resp = await client.post(`${ENDPOINT}/validate`, credential1);
+      expect(resp.status).toEqual(200);
+      expect(resp.data).toEqual({
+        message: 'Configuration is valid',

Review Comment:
   This `message` does not need assertion, as checking such constant strings is meaningless. You may switch to the `toMatchObject` API to assert fields other than `message`. Only fields expected to exist in the configuration will be checked.
   
   If no further assertion of the response is required, then only assert the response status code.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat: add validate API to standalone mode [apisix]

Posted by "Revolyssup (via GitHub)" <gi...@apache.org>.
Revolyssup commented on code in PR #12718:
URL: https://github.com/apache/apisix/pull/12718#discussion_r2480374431


##########
apisix/admin/standalone.lua:
##########
@@ -157,6 +158,104 @@ local function check_conf(checker, schema, item, typ)
     })
 end
 
+local function validate(ctx)

Review Comment:
   done
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat: add validate API to standalone mode [apisix]

Posted by "membphis (via GitHub)" <gi...@apache.org>.
membphis commented on code in PR #12718:
URL: https://github.com/apache/apisix/pull/12718#discussion_r2520995829


##########
apisix/admin/standalone.lua:
##########
@@ -347,20 +457,24 @@ local function head(ctx)
     return core.response.exit(200)
 end
 
-
 function _M.run()

Review Comment:
   another Lua style:
   
   ```lua
   function _M.run()
       local ctx = ngx.ctx.api_ctx
       local method = str_lower(get_method())
       if method == "put" then
           return update(ctx)
       end
   
       if method == "post" then
           local path = ctx.var.uri
           if path == "/apisix/admin/configs/validate" then
               return validate(ctx)
           else
               return core.response.exit(404, {error_msg = "Not found"})
           end
       end
   
       if method == "head" then
           return head(ctx)
       end
   
       -- default is get
       return get(ctx)
   end
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat: add validate API to standalone mode [apisix]

Posted by "AlinsRan (via GitHub)" <gi...@apache.org>.
AlinsRan commented on code in PR #12718:
URL: https://github.com/apache/apisix/pull/12718#discussion_r2554719605


##########
t/admin/standalone.spec.ts:
##########
@@ -194,6 +194,86 @@ describe('Admin - Standalone', () => {
   });
 
   describe('Normal', () => {
+    it('validate config (success case with json)', async () => {
+      const resp = await client.post(`${ENDPOINT}/validate`, config1);
+      expect(resp.status).toEqual(200);
+      expect(resp.data).toEqual({
+        message: 'Configuration is valid',
+        valid: true,
+      });
+    });
+
+    it('validate config (success case with yaml)', async () => {
+      const resp = await client.post(`${ENDPOINT}/validate`, YAML.stringify(config1), {
+        headers: { 'Content-Type': 'application/yaml' },
+      });
+      expect(resp.status).toEqual(200);
+      expect(resp.data).toEqual({
+        message: 'Configuration is valid',
+        valid: true,
+      });
+    });
+
+    it('validate config (success case with multiple resources)', async () => {
+      const multiResourceConfig = {
+        routes: [
+          {
+            id: 'r1',
+            uri: '/r1',
+            upstream: {
+              nodes: { '127.0.0.1:1980': 1 },
+              type: 'roundrobin',
+            },
+          },
+          {
+            id: 'r2',
+            uri: '/r2',
+            upstream: {
+              nodes: { '127.0.0.1:1980': 1 },
+              type: 'roundrobin',
+            },
+          },
+        ],
+        services: [
+          {
+            id: 's1',
+            upstream: {
+              nodes: { '127.0.0.1:1980': 1 },
+              type: 'roundrobin',
+            },
+          },
+        ],
+        routes_conf_version: 1,
+        services_conf_version: 1,
+      };
+
+      const resp = await client.post(`${ENDPOINT}/validate`, multiResourceConfig);
+      expect(resp.status).toEqual(200);
+      expect(resp.data).toEqual({
+        message: 'Configuration is valid',
+        valid: true,
+      });
+    });
+
+    it('validate config with consumer credentials', async () => {
+      const resp = await client.post(`${ENDPOINT}/validate`, credential1);
+      expect(resp.status).toEqual(200);
+      expect(resp.data).toEqual({
+        message: 'Configuration is valid',

Review Comment:
   fixed.



##########
t/admin/standalone.spec.ts:
##########
@@ -194,6 +194,86 @@ describe('Admin - Standalone', () => {
   });
 
   describe('Normal', () => {
+    it('validate config (success case with json)', async () => {
+      const resp = await client.post(`${ENDPOINT}/validate`, config1);
+      expect(resp.status).toEqual(200);
+      expect(resp.data).toEqual({
+        message: 'Configuration is valid',
+        valid: true,
+      });
+    });
+
+    it('validate config (success case with yaml)', async () => {
+      const resp = await client.post(`${ENDPOINT}/validate`, YAML.stringify(config1), {
+        headers: { 'Content-Type': 'application/yaml' },
+      });
+      expect(resp.status).toEqual(200);
+      expect(resp.data).toEqual({
+        message: 'Configuration is valid',
+        valid: true,
+      });
+    });
+
+    it('validate config (success case with multiple resources)', async () => {
+      const multiResourceConfig = {
+        routes: [
+          {
+            id: 'r1',
+            uri: '/r1',
+            upstream: {
+              nodes: { '127.0.0.1:1980': 1 },
+              type: 'roundrobin',
+            },
+          },
+          {
+            id: 'r2',
+            uri: '/r2',
+            upstream: {
+              nodes: { '127.0.0.1:1980': 1 },
+              type: 'roundrobin',
+            },
+          },
+        ],
+        services: [
+          {
+            id: 's1',
+            upstream: {
+              nodes: { '127.0.0.1:1980': 1 },
+              type: 'roundrobin',
+            },
+          },
+        ],
+        routes_conf_version: 1,
+        services_conf_version: 1,
+      };
+
+      const resp = await client.post(`${ENDPOINT}/validate`, multiResourceConfig);
+      expect(resp.status).toEqual(200);
+      expect(resp.data).toEqual({
+        message: 'Configuration is valid',
+        valid: true,
+      });
+    });
+
+    it('validate config with consumer credentials', async () => {
+      const resp = await client.post(`${ENDPOINT}/validate`, credential1);
+      expect(resp.status).toEqual(200);
+      expect(resp.data).toEqual({
+        message: 'Configuration is valid',
+        valid: true,
+      });
+    });
+
+    it('validate config does not persist changes', async () => {
+      // First validate a configuration
+      const validateResp = await client.post(`${ENDPOINT}/validate`, config1);
+      expect(validateResp.status).toEqual(200);
+
+      // Then check that the configuration was not persisted
+      const getResp = await client.get(ENDPOINT);
+      expect(getResp.data.routes).toBeUndefined();
+    });

Review Comment:
   fixed.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat: add validate API to standalone mode [apisix]

Posted by "nic-6443 (via GitHub)" <gi...@apache.org>.
nic-6443 commented on code in PR #12718:
URL: https://github.com/apache/apisix/pull/12718#discussion_r2480237582


##########
apisix/admin/standalone.lua:
##########
@@ -157,6 +158,104 @@ local function check_conf(checker, schema, item, typ)
     })
 end
 
+local function validate(ctx)

Review Comment:
   can we reuse this new `validate` function in exist `update` function to reduce code duplicate between those two functions?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat: add validate API to standalone mode [apisix]

Posted by "AlinsRan (via GitHub)" <gi...@apache.org>.
AlinsRan commented on code in PR #12718:
URL: https://github.com/apache/apisix/pull/12718#discussion_r2540973427


##########
apisix/admin/standalone.lua:
##########
@@ -347,20 +457,24 @@ local function head(ctx)
     return core.response.exit(200)
 end
 
-
 function _M.run()

Review Comment:
   fixed.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat: add validate API to standalone mode [apisix]

Posted by "membphis (via GitHub)" <gi...@apache.org>.
membphis commented on code in PR #12718:
URL: https://github.com/apache/apisix/pull/12718#discussion_r2541005501


##########
apisix/admin/standalone.lua:
##########
@@ -158,6 +157,128 @@ local function check_conf(checker, schema, item, typ)
 end
 
 
+local function validate_configuration(req_body, collect_all_errors)
+    local validation_results = {
+        valid = true,
+        errors = {}
+    }
+
+    for key, conf_version_key in pairs(ALL_RESOURCE_KEYS) do
+        local items = req_body[key]
+        local resource = resources[key] or {}
+
+        -- Validate conf_version_key if present
+        local new_conf_version = req_body[conf_version_key]
+        if new_conf_version and type(new_conf_version) ~= "number" then
+            local error_msg
+            if collect_all_errors then
+                error_msg = conf_version_key .. " must be a number, got " .. type(new_conf_version)
+            else
+                error_msg = conf_version_key .. " must be a number"
+            end
+
+            if not collect_all_errors then
+                return false, error_msg
+            end
+            validation_results.valid = false
+            table_insert(validation_results.errors, {
+                resource_type = key,
+                error = error_msg
+            })
+        end
+
+        if items and #items > 0 then
+            local item_schema = resource.schema
+            local item_checker = resource.checker
+            local id_set = {}
+
+            for index, item in ipairs(items) do
+                local item_temp = tbl_deepcopy(item)
+                local valid, err = check_conf(item_checker, item_schema, item_temp, key)
+                if not valid then
+                    local err_prefix = "invalid " .. key .. " at index " .. (index - 1) .. ", err: "
+                    local err_msg = type(err) == "table" and err.error_msg or err
+                    local error_msg = err_prefix .. err_msg
+
+                    if not collect_all_errors then
+                        return false, error_msg
+                    end
+                    validation_results.valid = false
+                    table_insert(validation_results.errors, {
+                        resource_type = key,
+                        index = index - 1,
+                        error = error_msg
+                    })
+                end
+
+                -- check for duplicate IDs
+                local duplicated, dup_err = check_duplicate(item, key, id_set)
+                if duplicated then
+                    if not collect_all_errors then
+                        return false, dup_err
+                    end
+                    validation_results.valid = false
+                    table_insert(validation_results.errors, {
+                        resource_type = key,
+                        index = index - 1,
+                        error = dup_err
+                    })
+                end
+            end
+        end
+    end
+
+    if collect_all_errors then

Review Comment:
   code style
   
   ```lua
       if collect_all_errors then
           return validation_results.valid, validation_results
       end
       
       return validation_results.valid



##########
apisix/admin/standalone.lua:
##########
@@ -158,6 +157,128 @@ local function check_conf(checker, schema, item, typ)
 end
 
 
+local function validate_configuration(req_body, collect_all_errors)
+    local validation_results = {
+        valid = true,
+        errors = {}
+    }
+
+    for key, conf_version_key in pairs(ALL_RESOURCE_KEYS) do
+        local items = req_body[key]
+        local resource = resources[key] or {}
+
+        -- Validate conf_version_key if present
+        local new_conf_version = req_body[conf_version_key]
+        if new_conf_version and type(new_conf_version) ~= "number" then
+            local error_msg
+            if collect_all_errors then
+                error_msg = conf_version_key .. " must be a number, got " .. type(new_conf_version)
+            else
+                error_msg = conf_version_key .. " must be a number"
+            end
+
+            if not collect_all_errors then
+                return false, error_msg
+            end
+            validation_results.valid = false
+            table_insert(validation_results.errors, {
+                resource_type = key,
+                error = error_msg
+            })
+        end
+
+        if items and #items > 0 then
+            local item_schema = resource.schema
+            local item_checker = resource.checker
+            local id_set = {}
+
+            for index, item in ipairs(items) do
+                local item_temp = tbl_deepcopy(item)
+                local valid, err = check_conf(item_checker, item_schema, item_temp, key)
+                if not valid then
+                    local err_prefix = "invalid " .. key .. " at index " .. (index - 1) .. ", err: "
+                    local err_msg = type(err) == "table" and err.error_msg or err
+                    local error_msg = err_prefix .. err_msg
+
+                    if not collect_all_errors then
+                        return false, error_msg
+                    end
+                    validation_results.valid = false
+                    table_insert(validation_results.errors, {
+                        resource_type = key,
+                        index = index - 1,
+                        error = error_msg
+                    })
+                end
+
+                -- check for duplicate IDs
+                local duplicated, dup_err = check_duplicate(item, key, id_set)
+                if duplicated then
+                    if not collect_all_errors then
+                        return false, dup_err
+                    end
+                    validation_results.valid = false
+                    table_insert(validation_results.errors, {
+                        resource_type = key,
+                        index = index - 1,
+                        error = dup_err
+                    })
+                end
+            end
+        end
+    end
+
+    if collect_all_errors then
+        return validation_results.valid, validation_results
+    else
+        return validation_results.valid, nil
+    end
+end
+
+local function validate(ctx)
+    local content_type = core.request.header(nil, "content-type") or "application/json"
+    local req_body, err = core.request.get_body()
+    if err then
+        return core.response.exit(400, {error_msg = "invalid request body: " .. err})
+    end
+
+    if not req_body or #req_body <= 0 then
+        return core.response.exit(400, {error_msg = "invalid request body: empty request body"})
+    end
+
+    local data
+    if core.string.has_prefix(content_type, "application/yaml") then
+        local ok, result = pcall(yaml.load, req_body, { all = false })
+        if not ok or type(result) ~= "table" then
+            err = "invalid yaml request body"
+        else
+            data = result
+        end
+    else
+        data, err = core.json.decode(req_body)
+    end
+
+    if err then
+        core.log.error("invalid request body: ", req_body, " err: ", err)
+        return core.response.exit(400, {error_msg = "invalid request body: " .. err})
+    end
+
+    local valid, validation_results = validate_configuration(data, true)
+
+    if valid then

Review Comment:
   ```lua
       if not valid then
           return core.response.exit(400, {
               error_msg = "Configuration validation failed",
               valid = false,
               errors = validation_results.errors
           })
       end
           
       return core.response.exit(200, {
             message = "Configuration is valid",
             valid = true
         })



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat: add validate API to standalone mode [apisix]

Posted by "AlinsRan (via GitHub)" <gi...@apache.org>.
AlinsRan commented on code in PR #12718:
URL: https://github.com/apache/apisix/pull/12718#discussion_r2508457735


##########
apisix/admin/standalone.lua:
##########
@@ -211,54 +333,44 @@ local function update(ctx)
         return core.response.exit(204)
     end
 
-    -- check input by jsonschema
+    local valid, error_msg = validate_configuration(req_body, false)
+    if not valid then
+        return core.response.exit(400, { error_msg = error_msg })
+    end
+
+    -- check input by jsonschema and build the final config
     local apisix_yaml = {}
 
     for key, conf_version_key in pairs(ALL_RESOURCE_KEYS) do
         local conf_version = config and config[conf_version_key] or 0
         local items = req_body[key]
         local new_conf_version = req_body[conf_version_key]
-        local resource = resources[key] or {}
-        if not new_conf_version then
-            new_conf_version = conf_version + 1
-        else
-            if type(new_conf_version) ~= "number" then
-                return core.response.exit(400, {
-                    error_msg = conf_version_key .. " must be a number",
-                })
-            end
+
+        if new_conf_version then
             if new_conf_version < conf_version then
                 return core.response.exit(400, {
                     error_msg = conf_version_key ..
                         " must be greater than or equal to (" .. conf_version .. ")",
                 })
             end
+        else
+            new_conf_version = conf_version + 1
         end
 
-
         apisix_yaml[conf_version_key] = new_conf_version
         if new_conf_version == conf_version then
             apisix_yaml[key] = config and config[key]
         elseif items and #items > 0 then
             apisix_yaml[key] = table_new(#items, 0)
-            local item_schema = resource.schema
-            local item_checker = resource.checker
             local id_set = {}
 
-            for index, item in ipairs(items) do
-                local item_temp = tbl_deepcopy(item)
-                local valid, err = check_conf(item_checker, item_schema, item_temp, key)
-                if not valid then
-                    local err_prefix = "invalid " .. key .. " at index " .. (index - 1) .. ", err: "
-                    local err_msg = type(err) == "table" and err.error_msg or err
-                    core.response.exit(400, { error_msg = err_prefix .. err_msg })
-                end
+            for _, item in ipairs(items) do
                 -- prevent updating resource with the same ID
                 -- (e.g., service ID or other resource IDs) in a single request
                 local duplicated, err = check_duplicate(item, key, id_set)

Review Comment:
   The logic of `check_deuplicate` exists twice, the first time in `validate_configuration`, and the second time is redundant.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat: add validate API to standalone mode [apisix]

Posted by "AlinsRan (via GitHub)" <gi...@apache.org>.
AlinsRan commented on code in PR #12718:
URL: https://github.com/apache/apisix/pull/12718#discussion_r2548257160


##########
apisix/admin/standalone.lua:
##########
@@ -158,6 +157,128 @@ local function check_conf(checker, schema, item, typ)
 end
 
 
+local function validate_configuration(req_body, collect_all_errors)
+    local validation_results = {
+        valid = true,
+        errors = {}
+    }
+
+    for key, conf_version_key in pairs(ALL_RESOURCE_KEYS) do
+        local items = req_body[key]
+        local resource = resources[key] or {}
+
+        -- Validate conf_version_key if present
+        local new_conf_version = req_body[conf_version_key]
+        if new_conf_version and type(new_conf_version) ~= "number" then
+            local error_msg
+            if collect_all_errors then
+                error_msg = conf_version_key .. " must be a number, got " .. type(new_conf_version)
+            else
+                error_msg = conf_version_key .. " must be a number"
+            end
+
+            if not collect_all_errors then
+                return false, error_msg
+            end
+            validation_results.valid = false
+            table_insert(validation_results.errors, {
+                resource_type = key,
+                error = error_msg
+            })
+        end
+
+        if items and #items > 0 then
+            local item_schema = resource.schema
+            local item_checker = resource.checker
+            local id_set = {}
+
+            for index, item in ipairs(items) do
+                local item_temp = tbl_deepcopy(item)
+                local valid, err = check_conf(item_checker, item_schema, item_temp, key)
+                if not valid then
+                    local err_prefix = "invalid " .. key .. " at index " .. (index - 1) .. ", err: "
+                    local err_msg = type(err) == "table" and err.error_msg or err
+                    local error_msg = err_prefix .. err_msg
+
+                    if not collect_all_errors then
+                        return false, error_msg
+                    end
+                    validation_results.valid = false
+                    table_insert(validation_results.errors, {
+                        resource_type = key,
+                        index = index - 1,
+                        error = error_msg
+                    })
+                end
+
+                -- check for duplicate IDs
+                local duplicated, dup_err = check_duplicate(item, key, id_set)
+                if duplicated then
+                    if not collect_all_errors then
+                        return false, dup_err
+                    end
+                    validation_results.valid = false
+                    table_insert(validation_results.errors, {
+                        resource_type = key,
+                        index = index - 1,
+                        error = dup_err
+                    })
+                end
+            end
+        end
+    end
+
+    if collect_all_errors then

Review Comment:
   fixed.



##########
apisix/admin/standalone.lua:
##########
@@ -158,6 +157,128 @@ local function check_conf(checker, schema, item, typ)
 end
 
 
+local function validate_configuration(req_body, collect_all_errors)
+    local validation_results = {
+        valid = true,
+        errors = {}
+    }
+
+    for key, conf_version_key in pairs(ALL_RESOURCE_KEYS) do
+        local items = req_body[key]
+        local resource = resources[key] or {}
+
+        -- Validate conf_version_key if present
+        local new_conf_version = req_body[conf_version_key]
+        if new_conf_version and type(new_conf_version) ~= "number" then
+            local error_msg
+            if collect_all_errors then
+                error_msg = conf_version_key .. " must be a number, got " .. type(new_conf_version)
+            else
+                error_msg = conf_version_key .. " must be a number"
+            end
+
+            if not collect_all_errors then
+                return false, error_msg
+            end
+            validation_results.valid = false
+            table_insert(validation_results.errors, {
+                resource_type = key,
+                error = error_msg
+            })
+        end
+
+        if items and #items > 0 then
+            local item_schema = resource.schema
+            local item_checker = resource.checker
+            local id_set = {}
+
+            for index, item in ipairs(items) do
+                local item_temp = tbl_deepcopy(item)
+                local valid, err = check_conf(item_checker, item_schema, item_temp, key)
+                if not valid then
+                    local err_prefix = "invalid " .. key .. " at index " .. (index - 1) .. ", err: "
+                    local err_msg = type(err) == "table" and err.error_msg or err
+                    local error_msg = err_prefix .. err_msg
+
+                    if not collect_all_errors then
+                        return false, error_msg
+                    end
+                    validation_results.valid = false
+                    table_insert(validation_results.errors, {
+                        resource_type = key,
+                        index = index - 1,
+                        error = error_msg
+                    })
+                end
+
+                -- check for duplicate IDs
+                local duplicated, dup_err = check_duplicate(item, key, id_set)
+                if duplicated then
+                    if not collect_all_errors then
+                        return false, dup_err
+                    end
+                    validation_results.valid = false
+                    table_insert(validation_results.errors, {
+                        resource_type = key,
+                        index = index - 1,
+                        error = dup_err
+                    })
+                end
+            end
+        end
+    end
+
+    if collect_all_errors then
+        return validation_results.valid, validation_results
+    else
+        return validation_results.valid, nil
+    end
+end
+
+local function validate(ctx)
+    local content_type = core.request.header(nil, "content-type") or "application/json"
+    local req_body, err = core.request.get_body()
+    if err then
+        return core.response.exit(400, {error_msg = "invalid request body: " .. err})
+    end
+
+    if not req_body or #req_body <= 0 then
+        return core.response.exit(400, {error_msg = "invalid request body: empty request body"})
+    end
+
+    local data
+    if core.string.has_prefix(content_type, "application/yaml") then
+        local ok, result = pcall(yaml.load, req_body, { all = false })
+        if not ok or type(result) ~= "table" then
+            err = "invalid yaml request body"
+        else
+            data = result
+        end
+    else
+        data, err = core.json.decode(req_body)
+    end
+
+    if err then
+        core.log.error("invalid request body: ", req_body, " err: ", err)
+        return core.response.exit(400, {error_msg = "invalid request body: " .. err})
+    end
+
+    local valid, validation_results = validate_configuration(data, true)
+
+    if valid then

Review Comment:
   fixed.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat: add validate API to standalone mode [apisix]

Posted by "Revolyssup (via GitHub)" <gi...@apache.org>.
Revolyssup commented on code in PR #12718:
URL: https://github.com/apache/apisix/pull/12718#discussion_r2480374431


##########
apisix/admin/standalone.lua:
##########
@@ -157,6 +158,104 @@ local function check_conf(checker, schema, item, typ)
     })
 end
 
+local function validate(ctx)

Review Comment:
   done
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat: add validate API to standalone mode [apisix]

Posted by "AlinsRan (via GitHub)" <gi...@apache.org>.
AlinsRan commented on code in PR #12718:
URL: https://github.com/apache/apisix/pull/12718#discussion_r2554719988


##########
apisix/admin/standalone.lua:
##########
@@ -158,6 +157,120 @@ local function check_conf(checker, schema, item, typ)
 end
 
 
+local function validate_configuration(req_body, collect_all_errors)
+    local validation_results = {
+        valid = true,
+        errors = {}
+    }
+
+    for key, conf_version_key in pairs(ALL_RESOURCE_KEYS) do
+        local items = req_body[key]
+        local resource = resources[key] or {}
+
+        -- Validate conf_version_key if present
+        local new_conf_version = req_body[conf_version_key]
+        if new_conf_version and type(new_conf_version) ~= "number" then
+            if not collect_all_errors then
+                return false, conf_version_key .. " must be a number"
+            end
+            validation_results.valid = false
+            table_insert(validation_results.errors, {
+                resource_type = key,
+                error = conf_version_key .. " must be a number, got " .. type(new_conf_version)
+            })
+        end
+
+        if items and #items > 0 then
+            local item_schema = resource.schema
+            local item_checker = resource.checker
+            local id_set = {}
+
+            for index, item in ipairs(items) do
+                local item_temp = tbl_deepcopy(item)
+                local valid, err = check_conf(item_checker, item_schema, item_temp, key)
+                if not valid then
+                    local err_prefix = "invalid " .. key .. " at index " .. (index - 1) .. ", err: "
+                    local err_msg = type(err) == "table" and err.error_msg or err
+                    local error_msg = err_prefix .. err_msg
+
+                    if not collect_all_errors then
+                        return false, error_msg
+                    end
+                    validation_results.valid = false
+                    table_insert(validation_results.errors, {
+                        resource_type = key,
+                        index = index - 1,
+                        error = error_msg
+                    })
+                end
+
+                -- check for duplicate IDs
+                local duplicated, dup_err = check_duplicate(item, key, id_set)
+                if duplicated then
+                    if not collect_all_errors then
+                        return false, dup_err
+                    end
+                    validation_results.valid = false
+                    table_insert(validation_results.errors, {
+                        resource_type = key,
+                        index = index - 1,
+                        error = dup_err
+                    })
+                end
+            end
+        end
+    end
+
+    if collect_all_errors then
+        return validation_results.valid, validation_results
+    else
+        return validation_results.valid, nil
+    end
+end
+
+local function validate(ctx)
+    local content_type = core.request.header(nil, "content-type") or "application/json"
+    local req_body, err = core.request.get_body()
+    if err then
+        return core.response.exit(400, {error_msg = "invalid request body: " .. err})
+    end
+
+    if not req_body or #req_body <= 0 then
+        return core.response.exit(400, {error_msg = "invalid request body: empty request body"})
+    end
+
+    local data
+    if core.string.has_prefix(content_type, "application/yaml") then
+        local ok, result = pcall(yaml.load, req_body, { all = false })
+        if not ok or type(result) ~= "table" then
+            err = "invalid yaml request body"
+        else
+            data = result
+        end
+    else
+        data, err = core.json.decode(req_body)
+    end
+
+    if err then
+        core.log.error("invalid request body: ", req_body, " err: ", err)
+        return core.response.exit(400, {error_msg = "invalid request body: " .. err})
+    end
+
+    local valid, validation_results = validate_configuration(data, true)
+    if not valid then
+        return core.response.exit(400, {
+            error_msg = "Configuration validation failed",
+            valid = false,
+            errors = validation_results.errors
+        })
+    end
+
+    return core.response.exit(200, {
+        message = "Configuration is valid",
+        valid = true
+    })

Review Comment:
   fixed.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat: add validate API to standalone mode [apisix]

Posted by "AlinsRan (via GitHub)" <gi...@apache.org>.
AlinsRan commented on code in PR #12718:
URL: https://github.com/apache/apisix/pull/12718#discussion_r2554720565


##########
t/admin/standalone.spec.ts:
##########
@@ -641,5 +721,228 @@ describe('Admin - Standalone', () => {
           'invalid routes at index 0, err: invalid configuration: failed to match pattern "^((uri|server_name|server_addr|request_uri|remote_port|remote_addr|query_string|host|hostname|mqtt_client_id)|arg_[0-9a-zA-z_-]+)$" with "args_invalid"',
       });
     });
+    it('validate config (duplicate route id)', async () => {

Review Comment:
   The target API for testing is different, here we are testing the validate API.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat: add validate API to standalone mode [apisix]

Posted by "Baoyuantop (via GitHub)" <gi...@apache.org>.
Baoyuantop merged PR #12718:
URL: https://github.com/apache/apisix/pull/12718


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org