# 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/v1/docs/apis-all/system-apis/system-generated-is-valid-data-for-table-api.html

Runs the [schema](/v1/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`](/v1/examples/sys/system/isValidDataForTable.html) |

## The call

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

```json title="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

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

- 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](/v1/docs/settings/systemApiSettings.html) give it `apiAccessType: TOKEN_ACCESS` (the token of an API user whose [group](/v1/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](/v1/docs/apis-all/hooks/preHook-api.html) run around it like around any API.
- The [request headers](/v1/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](/v1/docs/apis-all/overview.html) · [Response format](/v1/docs/apis-all/response-format.html) · [System API settings](/v1/docs/settings/systemApiSettings.html) · [System APIs from code](/v1/examples/sys/system/system.html)
