# Is Valid Data for Table API

> Check data against the schema of a table without saving it with the is valid data for table system API of API Maker - the same conversions and validations as a save or an update, the errors as the answer.

Source: https://docs.apimaker.dev/v4/docs/apis-all/system-apis/system-generated-is-valid-data-for-table-api.html

Runs the [schema](https://docs.apimaker.dev/v4/docs/schema/schema.html) 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`](https://docs.apimaker.dev/v4/docs/apis-all/system-apis/system-generated-is-valid-data-for-table-api.html#from-code) |

## The call

**Body**

```json
{
    "instance": "mongodb", "database": "shop", "collection": "products",
    "data": { "name": "M", "currency": "EUR", "price_cents": -5, "contact_email": "x@example.com" }
}
```

**Answer**

```json
{
    "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

**TypeScript**

```typescript
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; }
```

**Python**

```python
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
```

**PHP**

```php
$errors = $g->sys->system->isValidDataForTable('mongodb', 'shop', 'products', data: $g->req->body, sendingTo: 'SAVE');
if ($errors) {
    $g->res->statusCode = 400;
    return $errors;
}
```

**C#**

```csharp
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;
}
```

**Go**

```go
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
}
```

**Rust**

```rust
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));
}
```

**Java**

```java
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;
}
```

**Kotlin**

```kotlin
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.

## Access and settings

- Over HTTP, a system API answers once its [settings](https://docs.apimaker.dev/v4/docs/settings/systemApiSettings.html) give it `apiAccessType: TOKEN_ACCESS` (the token of an API user whose [group](https://docs.apimaker.dev/v4/docs/apis-security/api-group-permission.html) grants this system API, in `x-am-authorization`) or `IS_PUBLIC`. Without settings it is `NO_ACCESS` : your code calls it through `g.sys`, the admin panel tests it, and an HTTP call is refused.
- The settings can also cache the answer or require person tokens (`authProviders`) ; [pre and post hooks](https://docs.apimaker.dev/v4/docs/apis-all/hooks/preHook-api.html) run around it like around any API.
- The [request headers](https://docs.apimaker.dev/v4/docs/apis-all/header/requestHeader.html) apply : `x-am-response-case`, `x-am-content-type-response`, `x-am-internationalization`, `x-am-tenant-username`…

## Errors

| Code | When |
|---|---|
| `400` | The body is wrong : the message names the missing or invalid key, `Unable to find schema for …` for a table without a schema. |
| `401` | No valid API user token, or the API is `NO_ACCESS` : `You are not authorized to access this API.` |
| `403` | No group grants this system API. |

## Related

- [All APIs at a glance](https://docs.apimaker.dev/v4/docs/apis-all/overview.html) · [Response format](https://docs.apimaker.dev/v4/docs/apis-all/response-format.html) · [System API settings](https://docs.apimaker.dev/v4/docs/settings/systemApiSettings.html) · [System APIs from code](https://docs.apimaker.dev/v4/examples/sys/system/system.html)
