Skip to content

Commit

Permalink
add tooling for new TDs
Browse files Browse the repository at this point in the history
  • Loading branch information
egekorkan committed Dec 12, 2024
1 parent a03335e commit fada185
Show file tree
Hide file tree
Showing 4 changed files with 285 additions and 0 deletions.
95 changes: 95 additions & 0 deletions proposals/initial-connection/tooling/schema-new.schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type":"object",
"definitions": {
"singleSecurityDefinition":{
"type":"object",
"properties": {
"scheme":{
"type":"string"
}
}
},
"singleConnectionDefinition": {
"type": "object",
"properties": {
"base": {
"type": "string"
}
}
},
"singleFormDefinition": {
"type": "object",
"properties": {
"contentType": {
"type": "string"
},
"connection":{
"$ref": "#/definitions/singleConnectionDefinition"
}
}
},
"singleSchemaDefinition": {
"type": "object"
}
},
"properties": {
"security": {
"oneOf": [
{
"$ref": "#/definitions/singleSecurityDefinition"
},
{
"type": "string",
"$comment": "Reference to a securityDefinitions object key"
}
]
},
"form": {
"oneOf": [
{
"$ref": "#/definitions/singleFormDefinition"
},
{
"type": "string",
"$comment": "Reference to a formDefinitions object key"
}
]
},
"connection": {
"oneOf": [
{
"$ref": "#/definitions/singleConnectionDefinition"
},
{
"type": "string",
"$comment": "Reference to a connectionDefinitions object key"
}
]
},
"securityDefinitions": {
"type": "object",
"additionalProperties": {
"$ref": "#/definitions/singleSecurityDefinition"
}
},
"connectionDefinitions": {
"type": "object",
"additionalProperties": {
"$ref": "#/definitions/singleConnectionDefinition"
}
},
"formDefinitions": {
"type": "object",
"additionalProperties": {
"$ref": "#/definitions/singleFormDefinition"
}
},
"schemaDefinitions": {
"type": "object",
"additionalProperties": {
"$ref": "#/definitions/singleSchemaDefinition"
}
}
}
}
16 changes: 16 additions & 0 deletions proposals/initial-connection/tooling/td-transform.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Takes a TD with initial connection information and transforms it to a TD 1.1

const tdInput = {};
let tdOutput = {};

// Find if there is a default in the root level

if (Object.hasOwn(tdInput,"form")){
const defaultForm = tdInput.form;

}

// Go into the forms and try to find each reference or inline

// separating inline security to secDef -> create sec1 as object key
// separating inline schema to schema definitions -> create schema1 as object key
137 changes: 137 additions & 0 deletions proposals/initial-connection/tooling/tds.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
// TDs are provided as js so that we can put comments
// However, please do not remove JSON serialization so that they can be copy pasted easily

const validTDs = [
// Inline (no definitions objects)
// Separate Defaults
{
"title": "test1",
"connection":{
"base": "https://example.com"
},
"form":{
"contentType":"application/json"
},
"security":{
"scheme":"nosec"
},
"properties": {
"prop1": {
"type":"string",
"forms": [
{
"href": "/props/prop1"
}
]
},
"prop2": {
"type":"string",
"forms": [
{
"href": "/props/prop2"
}
]
}
}
},
// All Defaults in a Form but still with connection
{
"title": "test2",
"form":{
"contentType":"application/json",
"connection":{
"base": "https://example.com",
"security":{
"scheme":"nosec"
}
}
},
"properties": {
"prop1": {
"type":"string",
"forms": [
{
"href": "/props/prop1"
}
]
},
"prop2": {
"type":"string",
"forms": [
{
"href": "/props/prop2"
}
]
}
}
},
// All defaults in a form and flattened without connection
{
"title": "test3",
"form":{
"contentType":"application/json",
"base": "https://example.com",
"security":{
"scheme":"nosec"
}
},
"properties": {
"prop1": {
"type":"string",
"forms": [
{
"href": "/props/prop1"
}
]
},
"prop2": {
"type":"string",
"forms": [
{
"href": "/props/prop2"
}
]
}
}
}
]



// TODO: Invalid TDs are not detected yet
const invalidTDs = [
// Inline (no definitions objects)
// Missing Connection
{
"title": "test1",
"form": {
"contentType": 123 // FIXME: Put correct string value here
},
"security": {
"scheme": "nosec"
},
"properties": {
"prop1": {
"type": "string",
"forms": [
{
"href": "/props/prop1"
}
]
},
"prop2": {
"type": "string",
"forms": [
{
"href": "/props/prop2"
}
]
}
}
}
];

module.exports = {
validTDs,
invalidTDs,
};
37 changes: 37 additions & 0 deletions proposals/initial-connection/tooling/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const fs = require("fs");
const assert = require("assert");
const { validTDs, invalidTDs } = require("./tds");
const Ajv = require("ajv");

const tdSchema = fs.readFileSync("schema-new.schema.json");
const ajv = new Ajv({
strict: false,
addUsedSchema: false,
formats: {
// Do not validate the following formats.
// In the future we could provide a custom checking function for these formats
"uri-reference": true,
"json-pointer": true,
uri: true,
"date-time": true,
},
});

describe("Thing Description Confirmation", () => {

for (const [id, td] of validTDs.entries()) {
it(`should validate n° ${id}`, () => {
const valid = ajv.validate(JSON.parse(tdSchema), td);
assert.equal(valid, true, ajv.errorsText());
});
}
});

describe("Thing Description Rejection", () => {
for (const [id, td] of invalidTDs.entries()) {
it(`should NOT validate n° ${id}`, () => {
const valid = ajv.validate(JSON.parse(tdSchema), td);
assert.equal(valid, false, ajv.errorsText());
});
}
});

0 comments on commit fada185

Please sign in to comment.