Skip to content

Commit e1b6366

Browse files
author
Chris Harvey
committed
Merge branch 'alpha-v0.15'
2 parents bae1475 + 1883ded commit e1b6366

17 files changed

+2508
-1899
lines changed

README.md

+82-62
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# schemaorg-jsd
1+
# [schemaorg-jsd](https://chharvey.github.io/schemaorg-jsd/docs/api/)
22
JSON Schema validation for JSON-LD files using Schema.org vocabulary.
33

44

@@ -13,35 +13,55 @@ $ npm install schemaorg-jsd
1313

1414
This module exports an asynchronous validation function.
1515
It returns a Promise object, so you may use `await` or you may use standard `Promise` prototype methods.
16-
Read the JSDoc in `./index.js` for further details.
16+
Read the TypeDoc comments in `./src/index.ts` for further details.
1717

1818
```js
19-
const {sdoValidate} = require('schemaorg-jsd')
19+
const { sdoValidate } = require('schemaorg-jsd')
2020

2121
async function run() {
22-
// use any javascript object
23-
let school = {
24-
'@context': 'http://schema.org/',
25-
'@type': 'Place',
26-
name: `Blacksburg, ${usState('Virginia').code}`,
27-
}
28-
school['@id'] = 'http://www.blacksburg.gov/'
29-
try {
30-
let is_valid_place = sdoValidate(school, 'Place') // validate against the Place schema
31-
console.log(await is_valid_place) // return `true` if the document passes validation
32-
} catch (e) { // throw an `Error` if the document fails validation
33-
console.error(e)
34-
console.error(e.filename) // file where the invalidation occurred
35-
console.error(e.details) // more json-schema specifics; see <https://github.com/epoberezkin/ajv#validation-errors>
36-
}
37-
38-
// require a package
39-
let me = require('./me.json')
40-
await sdoValidate(me, 'Person')
41-
42-
// use a string (relative path) of the filename
43-
let org = './my-org.jsonld'
44-
await sdoValidate(org, 'Organization')
22+
// example 1: use any javascript object
23+
let school = {
24+
'@context': 'http://schema.org/',
25+
'@type': 'Place',
26+
name: `Blacksburg, ${usState('Virginia').code}`,
27+
}
28+
school['@id'] = 'http://www.blacksburg.gov/'
29+
try {
30+
let is_valid_place = sdoValidate(school, 'Place') // validate against the `Place` schema
31+
console.log(await is_valid_place) // return `true` if the document passes validation
32+
} catch (e) { // throw a `TypeError` if the document fails validation
33+
console.error(e)
34+
console.error(e.filename) // file where the invalidation occurred
35+
console.error(e.details) // more json-schema specifics; see <https://github.com/epoberezkin/ajv#validation-errors>
36+
}
37+
38+
// example 2: require a package
39+
let me = require('./me.json')
40+
console.log(await sdoValidate(me, 'Person')) // return `true` if the document passes validation
41+
42+
// example 3: use a string (relative path) of the filename
43+
let org = './my-org.jsonld'
44+
console.log(await sdoValidate(org, 'Organization')) // return `true` if the document passes validation
45+
46+
// example 4: infer the schema from the `'@type'` property
47+
await sdoValidate(school) // validates against the `Place` schema, since `school['@type'] === 'Place'`
48+
49+
// example 5: multiple types
50+
let business = {
51+
'@context': 'http://schema.org/',
52+
'@type': ['Place', 'LocalBusiness'],
53+
}
54+
await sdoValidate(business) // validates against all schemata in the array
55+
56+
// example 6: default type is `Thing` (http://schema.org/Thing)
57+
await sdoValidate({
58+
'@context': 'http://schema.org/',
59+
'@type': 'foobar' // validates against the `Thing` schema, since value 'foobar' cannot be found
60+
})
61+
await sdoValidate({
62+
'@context': 'http://schema.org/',
63+
// validates against the `Thing` schema, since property '@type' is missing
64+
})
4565
}
4666
```
4767

@@ -60,54 +80,54 @@ const Ajv = require('ajv')
6080
const sdo_jsd = require('schemaorg-jsd')
6181

6282
let my_schema = {
63-
"$schema": "http://json-schema.org/draft-07/schema#",
64-
"$id": "https://chharvey.github.io/example.jsd",
65-
"title": "Array<Thing>",
66-
"description": "An array of Schema.org Things.",
67-
"type": "array",
68-
"items": { "$ref": "https://chharvey.github.io/schemaorg-jsd/schema/Thing.jsd" }
83+
"$schema": "http://json-schema.org/draft-07/schema#",
84+
"$id": "https://chharvey.github.io/example.jsd",
85+
"title": "Array<Thing>",
86+
"description": "An array of Schema.org Things.",
87+
"type": "array",
88+
"items": { "$ref": "https://chharvey.github.io/schemaorg-jsd/schema/Thing.jsd" }
6989
}
7090
let my_data = [
71-
{ "@context": "http://schema.org/", "@type": "Thing", "name": "Thing 1" },
72-
{ "@context": "http://schema.org/", "@type": "Thing", "name": "Thing 2" }
91+
{ "@context": "http://schema.org/", "@type": "Thing", "name": "Thing 1" },
92+
{ "@context": "http://schema.org/", "@type": "Thing", "name": "Thing 2" }
7393
]
7494

7595
async function run() {
76-
const SCHEMATA = sdo_jsd.getSchemata()
77-
let ajv = new Ajv().addSchema(await SCHEMATA)
78-
ajv.validate(my_schema, my_data)
79-
// Note that the `Ajv#validate()` method’s parameters are reversed from this package’s `sdoValidate()`:
80-
// `Ajv#validate(schema, data)`
81-
// `sdoValidate(data, schemaTitle)`
96+
let ajv = new Ajv()
97+
.addMetaSchema(await sdo_jsd.META_SCHEMATA)
98+
.addSchema(await sdo_jsd.JSONLD_SCHEMA)
99+
.addSchema(await sdo_jsd.SCHEMATA)
100+
ajv.validate(my_schema, my_data)
101+
/*
102+
Note that the `Ajv#validate()` method’s parameters are reversed from this package’s `sdoValidate()`:
103+
104+
Ajv#validate(schema, data) // schema comes before data
105+
sdoValidate(data, schemaTitle) // data comes before schema
106+
*/
82107
}
83108
```
84109

85110
## View the “API”
86-
A set of [TypeDoc](http://typedoc.org/) declarations describing types and their properties.
87-
They are identical to the specs at [schema.org](https://schema.org/),
111+
This project includes a set of [TypeDoc](http://typedoc.org/) declarations describing types and their properties.
112+
They’re identical to the specs at [schema.org](https://schema.org/),
88113
but you can import the source code in your own project for
89114
[TypeScript](http://www.typescriptlang.org/) compilation.
90115

91-
```
92-
$ cd node_modules/schemaorg-jsd
93-
$ npm install
94-
$ npm run build
95-
$ cd -
96-
$ # open ./docs/api/index.html in your browser
97-
```
98-
**(Note: These docs will be published online soon, so you won’t have to build locally.)**
116+
[View the docs.](https://chharvey.github.io/schemaorg-jsd/docs/api/)
99117

100118
```ts
101-
import * as sdo from 'schemaorg-jsd' // TEMP: this import might change
119+
import * as sdo from 'schemaorg-jsd'
120+
102121
class Person {
103-
private _name: string
104-
/**
105-
* Construct a new Person object.
106-
* @param jsondata an object validating against the schemaorg-jsd `Person` schema
107-
*/
108-
constructor(jsondata: sdo.Person) {
109-
this._name = jsondata.name
110-
}
122+
/** This person’s name. */
123+
private _name: string;
124+
/**
125+
* Construct a new Person object.
126+
* @param jsondata an object validating against the schemaorg-jsd `Person` schema
127+
*/
128+
constructor(jsondata: sdo.Person) {
129+
this._name = jsondata.name
130+
}
111131
}
112132
```
113133

@@ -119,7 +139,7 @@ class Person {
119139
based off of the syntax used to define object literals in JavaScript.
120140

121141
## JSON Schema
122-
[JSON Schema](http://json-schema.org/) is a vocabulary, in JSON format,
142+
[JSON Schema](http://json-schema.org/) is a subset of JSON
123143
that allows you to validate JSON documents.
124144
In other words, a particular JSON schema tells you whether your JSON instance file is written correctly,
125145
if you choose to validate your instance against that schema.
@@ -137,7 +157,7 @@ Rather than everyone using their own data types, JSON-LD standardizes the markup
137157
for people and data types to communicate.
138158
JSON-LD has some rules, for example, an object’s `@id` property must be a string.
139159
Therefore, to enforce these rules, JSON-LD documents should validate against the
140-
[JSON-LD Schema](https://raw.githubusercontent.com/json-ld/json-ld.org/master/schemas/jsonld-schema.json).
160+
[JSON-LD Schema](https://json-ld.org/schemas/jsonld-schema.json).
141161
The official MIME Type of JSON-LD documents is `application/ld+json`,
142162
and JSON-LD files typically have file extension `.jsonld`.
143163

@@ -160,4 +180,4 @@ so that you can write a well-typed API for your project.
160180
You can semantically mark up your data using the Schema.org vocabulary with JSON-LD syntax.
161181
If you have a TypeScript API, you can import this project’s TypeScript to catch any type errors before runtime.
162182
Then, to prevent additional runtime errors or SEO mistakes, you can validate your markup against
163-
the JSON schemata (multiple “schemas”) in this project.
183+
the JSON schemata in this project.

config/tsconfig.json

-12
This file was deleted.

0 commit comments

Comments
 (0)