Kong / kong-python-pdk

Write Kong plugins in Python (Experimental)
Apache License 2.0
40 stars 14 forks source link

Custom Validation of Schema #45

Open ingvaldlorentzen opened 2 years ago

ingvaldlorentzen commented 2 years ago

Hi,

I'm unable to get custom validators for the Schema working.

The docs say:

Schema defines the config schema of plugin, it shares the same syntax as it’s a Lua plugin.

So I would assume this would work:

def some_validator():
    pass

Schema = ({'some_config': {'custom_validator': some_validator, 'type': 'string'}},)

But this produces the following error: TypeError: Object of type function is not JSON serializable

If I make the value a string, e.g.

def some_validator():
    pass

Schema = ({'some_config': {'custom_validator': 'some_validator', 'type': 'string'}},)

I get the following error: custom_validator = "expected a function"

How can I do custom validation with the Python PDK? My use case is to encrypt a field before it is stored, since the Python PDK cannot access environment variables.

In general a more complete example showing how the Lua schema is used in the Python PDK would be great!

ingvaldlorentzen commented 2 years ago

This is still an issue in the new 0.3.0 version.

fffonion commented 2 years ago

Hi @ingvaldlorentzen , right now we don't support custom validator in external plugins. You will need to do it on Python side (like in the __init__ function). I will also update the docs to reflect this.

shawnchapla-8451 commented 1 year ago

Hi @ingvaldlorentzen , right now we don't support custom validator in external plugins. You will need to do it on Python side (like in the __init__ function). I will also update the docs to reflect this.

Follow up question to this thread ... when adding plugin config validation code to the init function, what should be the behavior if a problem is found? The Python PDK documentation doesn't go into that detail ... does this function only run once when the plugin server starts or with every request? If the former, should the correct action be to throw an exception so the plugin server stops?