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

Is valid data for table

Runs the schema of a table on some data without writing anything, and answers the list of errors a save (or an update) would have refused it with. Empty list : the data is fine.

Method POST
URL /api/system-api/admin/is-valid-data-for-table : admin is the user path of your account
Body { instance, database, collection | table, data, sendingTo? } or an array of them
Answer data : an array of errors (empty when valid), or an array of arrays
From code g.sys.system.isValidDataForTable

The call

Body
{
    "instance": "mongodb", "database": "shop", "collection": "products",
    "data": { "name": "M", "currency": "EUR", "price_cents": -5, "contact_email": "[email protected]" }
}
Answer
{
    "success": true,
    "statusCode": 200,
    "data": [
        { "type": "schemaKeyNotFound", "field": "contact_email", "message": "Property not found in schema for key 'contact_email'.", "code": 400 },
        { "type": "minLength", "field": "name", "message": "Property 'name' should have minimum length of '2'.", "code": 400 },
        { "type": "enumValidation", "field": "currency", "message": "Property 'currency' should have any value from [IN, US, GB].", "code": 400 },
        { "type": "min", "field": "price_cents", "message": "Please provide minimum '0' for 'price_cents' field.", "code": 400 }
    ]
}
Key Meaning
instance, database, collection or table The table whose schema is used. A table without a schema is refused.
data One object, or an array of objects : then the answer is an array of error lists, one per object.
sendingTo SAVE (default) : required fields must be there. UPDATE : only the fields present are checked, the way update by id checks them.
  • The call is success: true even when the data is not : the errors are the answer, not a failure of the call.
  • An array of bodies checks several tables in one call.

From code

const errors = await g.sys.system.isValidDataForTable({ instance: 'mongodb', database: 'shop', collection: 'products', data: g.req.body, sendingTo: 'SAVE' });
if (errors.length) { g.res.statusCode = T.EStatusCode.BAD_REQUEST; return errors; }
errors = await g.sys.system.is_valid_data_for_table('mongodb', 'shop', 'products', data=g.req.body, sending_to='SAVE')
if errors:
    g.res.status_code = 400
    return errors
$errors = $g->sys->system->isValidDataForTable('mongodb', 'shop', 'products', data: $g->req->body, sendingTo: 'SAVE');
if ($errors) {
    $g->res->statusCode = 400;
    return $errors;
}
var errors = await g.Sys.System.IsValidDataForTable(new IsValidDataForTable("mongodb", "shop", "products") { Data = g.Req.Body.DeepClone(), SendingTo = "SAVE" });
if (errors.Count > 0)
{
    g.Res.StatusCode = 400;
    return errors;
}
errors, err := g.Sys.System.IsValidDataForTable(am.IsValidDataForTable{Instance: "mongodb", Database: "shop", Collection: "products", Data: g.Req.Body(), SendingTo: "SAVE"})
if err != nil {
    return nil, err
}
if len(errors) > 0 {
    g.Res.SetStatusCode(400)
    return errors, nil
}
let errors = g.sys.system.is_valid_data_for_table(IsValidDataForTable::new("mongodb", "shop", "products").data(g.req.body().clone()).sending_to("SAVE"))?;
if !errors.is_empty() {
    g.res.set_status_code(400);
    return Ok(json!(errors));
}
List<ResponseError> errors = g.sys.system.isValidDataForTable(new IsValidDataForTable("mongodb", "shop", "products").data(g.req.getBody()).sendingTo("SAVE"));
if (!errors.isEmpty()) {
    g.res.setStatusCode(400);
    return errors;
}
val errors = g.sys.system.isValidDataForTable(IsValidDataForTable("mongodb", "shop", "products").data(g.req.body).sendingTo("SAVE"))
if (errors.isNotEmpty()) {
    g.res.statusCode = 400
    return errors
}
  • Useful for a wizard which validates each step before the final save, or for an import which reports every bad row before writing any.

A table without a schema answers 400 : Unable to find schema for ….

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