Officially out now: The TypeDB 3.0 Roadmap

Lesson 4.5: Data validation

In Lesson 3.5, 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

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

studio run Try running

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.

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

studio run Try running

match
$book isa book, has $isbn;
$isbn "9781489962287" isa isbn;
delete
$book has $isbn;

studio run Try running

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

studio run Try running

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, and then explore how TypeDB validates data against the schema in Lesson 7.1.