You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: pages/blog/posts/advanced-json-schema-with-slashdb.md
+20-17Lines changed: 20 additions & 17 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -32,7 +32,7 @@ We will be examining how an application can leverage these features to thoroughl
32
32
33
33
In addition to creating endpoints for data, **SlashDB** has JSON schema endpoints so that you may examine the structure of any resource.
34
34
35
-
It has two modes of operation -*Data Discovery* and *SQL Pass-Thru*.
35
+
It has two modes of operation —*Data Discovery* and *SQL Pass-Thru*.
36
36
37
37
*Data Discovery* exposes unique REST endpoints for every resource within a database, from full tables, to subsets of tables, to individual columns or values.
38
38
@@ -41,20 +41,20 @@ It has two modes of operation - *Data Discovery* and *SQL Pass-Thru*.
41
41
42
42
#### Example Data Discovery Endpoints
43
43
44
-
* a database endpoint -https://demo.slashdb.com/db/Chinook
44
+
* a database endpoint —https://demo.slashdb.com/db/Chinook
45
45
46
-
* a table endpoint -https://demo.slashdb.com/db/Chinook/Employee
46
+
* a table endpoint —https://demo.slashdb.com/db/Chinook/Employee
47
47
48
-
* a single record, identified by primary key -https://demo.slashdb.com/db/Chinook/Employee/EmployeeId/1
48
+
* a single record, identified by primary key —https://demo.slashdb.com/db/Chinook/Employee/EmployeeId/1
49
49
50
50
51
51
*Data Discovery* also uses JSON schemas to validate data when records are being created or changed.
52
52
53
53
#### Example SQL Pass-Thru Endpoints
54
54
55
-
* a simple query -https://demo.slashdb.com/query/sales-by-year.json?limit=29
55
+
* a simple query —https://demo.slashdb.com/query/sales-by-year.json?limit=29
56
56
57
-
* a *parameterized* query, with a parameter-value pair of `year/2010`-https://demo.slashdb.com/query/invoices-by-year/year/2010?limit=29
57
+
* a *parameterized* query, with a parameter-value pair of `year/2010`—https://demo.slashdb.com/query/invoices-by-year/year/2010?limit=29
Examining this schema and comparing to the data, we can see that various JSON schema properties are present:
200
200
201
-
* there are `required` fields, specifying which fields in the record cannot not be null - this attribute also contains the table's primary key, in this case `EmployeeId`
201
+
* there are `required` fields, specifying which fields in the record cannot not be null — this attribute also contains the table's primary key, in this case `EmployeeId`
202
202
203
203
* some `string` fields have the `maxLength` property, reflecting that a maximum length is defined for the database column
204
204
205
-
* some `string` fields have the `format` property, that identifies them as `date-time` fields - validation/parsing tools can use this to ensure data validity
205
+
* some `string` fields have the `format` property, that identifies them as `date-time` fields — validation/parsing tools can use this to ensure data validity
206
206
207
-
* a `type` with an array of values means the field can be any one of those types - e.g. `["string", "null"]` is a string field that is nullable
207
+
* a `type` with an array of values means the field can be any one of those types — e.g. `["string", "null"]` is a string field that is nullable
208
+
209
+
While this approach is not typical—and not always the most appropriate — we support two ways for a property to be absent: it can be explicitly set to null, or it can be omitted entirely. This can help reduce bandwidth when transmitting data and aligns with XML conventions, which SlashDB also supports and which similarly allow a tag to be either "nillable" or omitted.
In this last endpoint, the schema is the same but is nested in an `array` of `items` since there are multiple records. Additionally, the JSON schema `minItems` property is present to denote that data being validated against this schema should contain at least one record.
325
328
326
329
One other parameter we can use with **SlashDB**'s schemas is the `cardinality` query string parameter:
@@ -426,9 +429,9 @@ And the schema:
426
429
}
427
430
```
428
431
429
-
In the schema, all records are grouped inside of an `array` of `items`, with an object property for each column in the results. Note that in *SQL Pass-Thru*, the `type` will always be an array of values - the first value being the actual type, the second being `null`; this allows the schema to account for NULL values in the result.
432
+
In the schema, all records are grouped inside of an `array` of `items`, with an object property for each column in the results. Note that in *SQL Pass-Thru*, the `type` will always be an array of values — the first value being the actual type, the second being `null`; this allows the schema to account for NULL values in the result.
430
433
431
-
You can now use this schema to examine the structure of the query's output, and also to validate data with any client-side tools that support JSON schemas.
434
+
You can now use this schema to examine the structure of the query's output, and also to validate data with any client—side tools that support JSON schemas.
432
435
433
436
### SlashDB Internal API & Schemas
434
437
@@ -442,7 +445,7 @@ A couple more JSON schema properties are used in internal schemas: `pattern` and
442
445
443
446
### Example Endpoints
444
447
445
-
* a query definition -https://demo.slashdb.com/querydef/active-users
448
+
* a query definition —https://demo.slashdb.com/querydef/active-users
We see that the `pattern` attribute is present in several portions of the schema here. Used in conjunction with `string`, `pattern` allows you to specify a regular expression that a value must match against.
593
596
594
-
E.g - the `query_id` attribute, which is a unique string that identifies the query, must be no more than 128 characters long (`maxLength` property), and the string must match [this regular expression](https://regex101.com/r/2crKdN/2)-`^(?![xX][mM][lL])[a-zA-Z_][a-zA-Z0-9@_~-]`.
597
+
For example, the `query_id` attribute, which is a unique string that identifies the query, must be no more than 128 characters long (`maxLength` property), and the string must match [this regular expression](https://regex101.com/r/2crKdN/2)—`^(?![xX][mM][lL])[a-zA-Z_][a-zA-Z0-9@_~-]`.
595
598
596
-
We also see the `oneOf` attribute - used here to define that an attribute is either null/not present, or an array of `items` (e.g. the `read` or `write` properties).
599
+
We also see the `oneOf` attribute — used here to define that an attribute is either null/not present, or an array of `items` (e.g. the `read` or `write` properties).
597
600
598
601
### Conclusion: JSON Schema as a Bridge Between Data and APIs
0 commit comments