Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
139 changes: 138 additions & 1 deletion JSONiq-tutorial-6.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -759,12 +759,149 @@
"return $my-date.da + $my-date.du"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can also use `@` to impose primary key restriction for objects inside an array.\n",
"\n",
"For example, the following object will not pass schema validation, because the primary key `id` is duplicated."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
"source": [
"%%jsoniq\n",
"declare type students as {\n",
" \"students\": [\n",
" {\n",
" \"@id\": \"integer\",\n",
" \"name\": \"string\",\n",
" \"age\": \"integer\"\n",
" }\n",
" ]\n",
"};\n",
"\n",
"let $my-student := validate type students {\n",
"{\n",
" \"students\": [\n",
" {\n",
" \"id\" : 1,\n",
" \"name\" : \"John Doe\",\n",
" \"age\" : 21\n",
" },\n",
" {\n",
" \"id\" : 1,\n",
" \"name\" : \"Jane Doe\",\n",
" \"age\" : 22\n",
" }\n",
" ]\n",
"}\n",
"}\n",
"return $my-student"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Or use `?` to allow a nullable field. The following object will not pass schema validation because the field `age` is null in the first student. By default, JSONiq considers JSON null values in an object as absent for validation against an optional key. The following cell will first disable this behavior, and then perform the validation.\n",
"\n",
"Try adding `?` after `age` in the schema definition to allow null values (`age` --> `age?`)."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from jsoniq import RumbleSession\n",
"rumble = RumbleSession.builder.getOrCreate();\n",
"conf = rumble.getRumbleConf()\n",
"conf.setLaxJSONNullValidation(False) # Disable lax validation for JSON nulls"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%%jsoniq\n",
"declare type students as {\n",
" \"students\": [\n",
" {\n",
" \"@id\": \"integer\",\n",
" \"name\": \"string\",\n",
" \"age\": \"integer\"\n",
" }\n",
" ]\n",
"};\n",
"\n",
"let $my-student := validate type students {\n",
"{\n",
" \"students\": [\n",
" {\n",
" \"id\" : 1,\n",
" \"name\" : \"John Doe\",\n",
" \"age\" : null\n",
" },\n",
" {\n",
" \"id\" : 2,\n",
" \"name\" : \"Jane Doe\",\n",
" \"age\" : 22\n",
" }\n",
" ]\n",
"}\n",
"}\n",
"return $my-student"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Lastly, `=` allows to define default values for missing fields. For example, the following schema definition will assign a default value of 18 to the field `age` if it is missing in a student object. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%%jsoniq\n",
"declare type students as {\n",
" \"students\": [\n",
" {\n",
" \"@id\": \"integer\",\n",
" \"name\": \"string\",\n",
" \"age\": \"integer=18\"\n",
" }\n",
" ]\n",
"};\n",
"\n",
"let $my-student := validate type students {\n",
"{\n",
" \"students\": [\n",
" {\n",
" \"id\" : 1,\n",
" \"name\" : \"John Doe\"\n",
" },\n",
" {\n",
" \"id\" : 2,\n",
" \"name\" : \"Jane Doe\",\n",
" \"age\" : 22\n",
" }\n",
" ]\n",
"}\n",
"}\n",
"return $my-student"
]
},
{
"cell_type": "markdown",
Expand Down