schemaorg-jsd
Version:
JSON Schema validation for JSON-LD files using Schema.org vocabulary.
104 lines (103 loc) • 4.31 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.SDOPropertyLD = exports.SDOClassLD = exports.SDODatatypeLD = exports.SDO_LD = void 0;
const path = require("path");
const url = require("url");
function label(jsd) {
return path.parse(new url.URL(jsd.title).pathname).name;
}
function linklist(lds) {
return lds.map((obj) => ` * - {@link ${obj['@id'].split(':')[1]}}`).join('\n');
}
class SDO_LD {
constructor(jsd) {
this['@id'] = `sdo:${label(jsd)}`;
this['rdfs:label'] = label(jsd);
this['rdfs:comment'] = jsd.description;
}
}
exports.SDO_LD = SDO_LD;
class SDODatatypeLD extends SDO_LD {
constructor(jsd) {
super(jsd);
this['@type'] = 'rdfs:Datatype';
}
static toTS(datatypeLD) {
return `
/**
* ${datatypeLD['rdfs:comment']}
*
* @see http://schema.org/${datatypeLD['rdfs:label']}
*/
export type ${datatypeLD['rdfs:label']} = ${new Map([
['Boolean', 'boolean'],
['Date', 'string'],
['DateTime', 'string'],
['Integer', 'number'],
['Number', 'number'],
['Text', 'string'],
['Time', 'string'],
['URL', 'string'],
]).get(datatypeLD['rdfs:label'])};
`.replace(/\n\t\t\t/g, '\n');
}
}
exports.SDODatatypeLD = SDODatatypeLD;
class SDOClassLD extends SDO_LD {
constructor(jsd, propertybase) {
super(jsd);
this['@type'] = 'rdfs:Class';
this.superClassOf = [];
this.valueOf = [];
this['rdfs:subClassOf'] = (label(jsd) !== 'Thing') ? { '@id': `sdo:${path.parse(jsd.allOf[0].$ref).name}` } : null;
this['rdfs:member'] = Object.entries(jsd.allOf[1].properties).map((entry) => {
const prop_name = entry[0];
return (propertybase.find((sch) => sch.title === `http://schema.org/${prop_name}`))
? { '@id': `sdo:${prop_name}` }
: (() => { throw new ReferenceError(`No corresponding jsd file was found for member subschema \`${label(jsd)}#${prop_name}\`.`); })();
});
}
static toTS(classLD) {
return `
/**
* ${classLD['rdfs:comment']}
*
* ${(classLD['superClassOf'].length) ? `*(Non-Normative):* Known subclasses:\n${linklist(classLD['superClassOf'])}\n` : ''}
* ${(classLD['valueOf'].length) ? `*(Non-Normative):* May appear as values of:\n${linklist(classLD['valueOf']).replace(/}/g, '_type}')}\n` : ''}
* @see http://schema.org/${classLD['rdfs:label']}
*/
export interface ${classLD['rdfs:label']} extends ${(classLD['rdfs:subClassOf']) ? classLD['rdfs:subClassOf']['@id'].split(':')[1] : 'NodeObject'} {
${classLD['rdfs:member'].map((member) => member['@id'].split(':')[1]).map((name) => `
${name}?: ${name}_type;
`.trim()).join('\n\t')}
}
`.replace(/\n\t\t\t/g, '\n');
}
}
exports.SDOClassLD = SDOClassLD;
class SDOPropertyLD extends SDO_LD {
constructor(jsd) {
super(jsd);
this['@type'] = 'rdf:Property';
this['rdfs:domain'] = [];
this.superPropertyOf = [];
this['rdfs:subPropertyOf'] = (jsd.allOf[0] !== true) ? { '@id': `sdo:${path.parse(jsd.allOf[0].$ref).name.split('.')[0]}` } : null;
this['rdfs:range'] = jsd.definitions.ExpectedType.anyOf.map((schema) => ({ '@id': `sdo:${path.parse(schema.$ref).name}` }));
this.$rangeArray = jsd.allOf[1].anyOf.length === 2;
}
static toTS(propertyLD) {
const rangeunion = `${propertyLD['rdfs:range'].map((cls) => cls['@id'].split(':')[1]).join(' | ')}`;
return `
/**
* ${propertyLD['rdfs:comment']}
*
* ${(propertyLD['rdfs:subPropertyOf']) ? `Extends {@link ${propertyLD['rdfs:subPropertyOf']['@id'].split(':')[1]}}` : ''}
* ${(propertyLD['superPropertyOf'].length) ? `*(Non-Normative):* Known subproperties:\n${linklist(propertyLD['superPropertyOf'])}\n` : ''}
* ${(propertyLD['rdfs:domain'].length) ? `*(Non-Normative):* Property of:\n${linklist(propertyLD['rdfs:domain'])}\n` : ''}
* @see http://schema.org/${propertyLD['rdfs:label']}
*/
type ${propertyLD['rdfs:label']}_type = ${rangeunion}${(propertyLD['$rangeArray']) ? ` | (${rangeunion})[]` : ''};
`.replace(/\n\t\t\t/g, '\n');
}
}
exports.SDOPropertyLD = SDOPropertyLD;