Skip to content

Commit 9e30777

Browse files
committed
explain string/null (jdesrosiers) + editorial fixes
1 parent f395803 commit 9e30777

File tree

1 file changed

+20
-17
lines changed

1 file changed

+20
-17
lines changed

pages/blog/posts/advanced-json-schema-with-slashdb.md

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ We will be examining how an application can leverage these features to thoroughl
3232

3333
In addition to creating endpoints for data, **SlashDB** has JSON schema endpoints so that you may examine the structure of any resource.
3434

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*.
3636

3737
*Data Discovery* exposes unique REST endpoints for every resource within a database, from full tables, to subsets of tables, to individual columns or values.
3838

@@ -41,20 +41,20 @@ It has two modes of operation - *Data Discovery* and *SQL Pass-Thru*.
4141

4242
#### Example Data Discovery Endpoints
4343

44-
* a database endpoint - https://demo.slashdb.com/db/Chinook
44+
* a database endpoint https://demo.slashdb.com/db/Chinook
4545

46-
* a table endpoint - https://demo.slashdb.com/db/Chinook/Employee
46+
* a table endpoint https://demo.slashdb.com/db/Chinook/Employee
4747

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
4949

5050

5151
*Data Discovery* also uses JSON schemas to validate data when records are being created or changed.
5252

5353
#### Example SQL Pass-Thru Endpoints
5454

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
5656

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
5858

5959
### Examining Data & Schemas
6060

@@ -94,7 +94,7 @@ https://demo.slashdb.com/db/Chinook/Employee/EmployeeId/1.json
9494
}
9595
```
9696

97-
We see that this record has data fields of various kinds - numbers, strings, dates, null values, as well as some additional metadata.
97+
We see that this record has data fields of various kinds numbers, strings, dates, null values, as well as some additional metadata.
9898

9999
Now let's retrieve the schema for this record:
100100
https://demo.slashdb.com/db/Chinook/Employee/EmployeeId/1.json?schema
@@ -198,21 +198,23 @@ https://demo.slashdb.com/db/Chinook/Employee/EmployeeId/1.json?schema
198198

199199
Examining this schema and comparing to the data, we can see that various JSON schema properties are present:
200200

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`
202202

203203
* some `string` fields have the `maxLength` property, reflecting that a maximum length is defined for the database column
204204

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
206206

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.
208210

209211
#### Endpoints With Multiple Records
210212

211-
To pull multiple records:
213+
To pull multiple records (table) you would use:
212214

213215
https://demo.slashdb.com/db/Chinook/Employee.json?limit=3
214216

215-
And the schema:
217+
To get corresponding schema:
216218

217219
https://demo.slashdb.com/db/Chinook/Employee.json?schema
218220

@@ -321,6 +323,7 @@ https://demo.slashdb.com/db/Chinook/Employee.json?schema
321323
"minItems": 1
322324
}
323325
```
326+
324327
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.
325328

326329
One other parameter we can use with **SlashDB**'s schemas is the `cardinality` query string parameter:
@@ -426,9 +429,9 @@ And the schema:
426429
}
427430
```
428431

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.
430433

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 clientside tools that support JSON schemas.
432435

433436
### SlashDB Internal API & Schemas
434437

@@ -442,7 +445,7 @@ A couple more JSON schema properties are used in internal schemas: `pattern` and
442445

443446
### Example Endpoints
444447

445-
* a query definition - https://demo.slashdb.com/querydef/active-users
448+
* a query definition https://demo.slashdb.com/querydef/active-users
446449

447450
### API & Schemas
448451

@@ -591,9 +594,9 @@ https://demo.slashdb.com/querydef/active-users.json?schema
591594

592595
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.
593596

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@_~-]`.
595598

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).
597600

598601
### Conclusion: JSON Schema as a Bridge Between Data and APIs
599602

0 commit comments

Comments
 (0)