Skip to content
This page View Markdown Open in ChatGPT Open in Claude

Is valid data for custom API

Runs the reqBodySchema or reqQueryParametersSchema of a custom API on some data and answers the errors the API would refuse it with, without running the API.

Method POST
URL /api/system-api/admin/is-valid-data-for-custom-api : admin is the user path of your account
Body { name, type, data } or an array of them
Answer data : an array of errors (empty when valid), or an array of arrays
From code g.sys.system.isValidDataForCustomAPI

The custom API settings

Basic info of the custom API
1
2
3
4
5
6
7
let customApi: T.ICustomApiSettingsTypes = {
    name: 'Create Customer', path: '/customers', requestMethod: T.ERequestMethod.POST, errorList: [],
    reqBodySchema: {
        first_name: { __type: T.EType.string, validations: { required: true } },
        phone: { __type: T.EType.number, validations: { required: true, min: 5, max: 999 } },
    },
};

The call

Body
{ "name": "Create Customer", "type": "BODY", "data": { "phone": 3 } }
Answer
{
    "success": true,
    "statusCode": 200,
    "data": [
        { "type": "required", "field": "first_name", "message": "Please provide valid 'first_name' field with type 'string'.", "code": 400 },
        { "type": "min", "field": "phone", "message": "Please provide minimum '5' for 'phone' field.", "code": 400 }
    ]
}
Key Meaning
name The name of the custom API.
type BODY for reqBodySchema, QUERY_PARAMS for reqQueryParametersSchema.
data The body or the query params to check.

From code

const draft = g.req.body;
const errors = await g.sys.system.isValidDataForCustomAPI({ name: 'Create Customer', type: T.ECustomAPIDataValidationType.BODY, data: draft });
draft = g.req.body
errors = await g.sys.system.is_valid_data_for_custom_api('Create Customer', type='BODY', data=draft)
$draft = $g->req->body;
$errors = $g->sys->system->isValidDataForCustomAPI('Create Customer', type: 'BODY', data: $draft);
var draft = g.Req.Body;
var errors = await g.Sys.System.IsValidDataForCustomAPI(new IsValidDataForCustomAPI("Create Customer") { Type = "BODY", Data = draft.DeepClone() });
draft := g.Req.Body()
errors, err := g.Sys.System.IsValidDataForCustomAPI(am.IsValidDataForCustomAPI{Name: "Create Customer", Type: "BODY", Data: draft})
if err != nil {
    return nil, err
}
let draft = g.req.body().clone();
let errors = g.sys.system.is_valid_data_for_custom_api(IsValidDataForCustomAPI::new("Create Customer").type_("BODY").data(draft))?;
JsonObject draft = g.req.getBody();
List<ResponseError> errors = g.sys.system.isValidDataForCustomAPI(new IsValidDataForCustomAPI("Create Customer").type("BODY").data(draft));
val draft = g.req.body
val errors = g.sys.system.isValidDataForCustomAPI(IsValidDataForCustomAPI("Create Customer").type("BODY").data(draft))

An unknown custom API answers 400 : Unable to find custom API with name ….

Access, headers and errors

  • Over HTTP, a system API answers only once its settings open it : TOKEN_ACCESS (the token of an API user whose group grants this system API) or IS_PUBLIC. Without settings it is NO_ACCESS : only your code (g.sys) and the admin panel reach it.
  • The settings can also keep the answer in the cache, or ask for the token of a person (authProviders). Pre and post hooks run around it, as around any API.
  • It takes the request headers and answers the envelope. 401 : no valid token, or the API is NO_ACCESS. 403 : no group grants it. 400 : the body is wrong, and the message names the key.

Related : All APIs · System API settings · System APIs from code