# Lesson 4.5: Data validation

## [](#_blank_heading)

In [Lesson 3.5](../../3-reading-data/3.5-query-validation/index.md), we saw that TypeDB validates all read queries against the schema to ensure semantic correctness. Likewise, all write queries are also validated to ensure data integrity. We’ve already seen an example of an invalid query in [Lesson 4.2](../4.2-inserting-polymorphic-data/index.md)

```typeql
match
$us isa country, has name "United States";
$ga isa state, has name "Georgia";
insert
locating ($us, $ga);
```

In this case, the query was invalid because TypeDB cannot infer the roles that `$us` and `$ga` should play, but there are many reasons a query can be invalid. Try running each of the following ones to see what happens.

```typeql
match
$odyssey isa ebook, has isbn "9780393634563";
insert
$odyssey has stock 20;
```

```typeql
match
$book isa book, has $isbn;
$isbn isa isbn "9781489962287";
delete
has $isbn of $book;
```

```typeql
match
$order-28 isa order, has id "o0028";
$paid isa status "paid";
delete
has $paid of $order-28;
insert
$order-28 has status "sent";
```

Exercise

Examine the three queries above and the error messages returned to determine the reason why each one is not semantically sound.

Answer

*   The first query attempts to give a stock level to an ebook, but ebooks do not have stock levels. In the schema, only `paperback` and `hardback` own `stock`.
    
*   The second query attempts to delete a book’s ISBN-13, but all books must have an ISBN-13. In the schema, `isbn-13` is a key attribute of `book`.
    
*   The third query attempts to change the status of order "o0028" to "sent", but this is not a permitted value for the status. In the schema, the value of `status` has a regex constraint applied.
    

We’ll see how we can use the schema to constrain inserted data in [Lesson 5](../../5-defining-schemas/index.md), and then explore how TypeDB validates data against the schema in [Lesson 7.1](../../7-understanding-query-patterns/7.1-patterns-as-constraints/index.md).

[Updating data](../4.4-updating-data/index.md) [Summary](../summary/index.md)

[Edit on GitHub](https://github.com/typedb/typedb-docs/edit/3.x-development/academy/modules/ROOT/pages/4-writing-data/4.5-data-validation.adoc) Edit this page on GitHub.