Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

plugin: orama schema from json schema #453

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions packages/plugin-jsonschema/LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Copyright 2023 OramaSearch Inc

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
13 changes: 13 additions & 0 deletions packages/plugin-jsonschema/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Nextra Plugin
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wrong title


[![Tests](https://github.com/oramasearch/orama/actions/workflows/turbo.yml/badge.svg)](https://github.com/oramasearch/orama/actions/workflows/turbo.yml)

Official plugin to convert schemas in the jsonschema format to work with Orama.

# Usage

For the complete usage guide, please refer to the [official plugin documentation](https://docs.oramasearch.com/plugins/plugin-nextra).

# License

[Apache-2.0](/LICENSE.md)
50 changes: 50 additions & 0 deletions packages/plugin-jsonschema/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"name": "@orama/plugin-jsonschema",
"version": "1.1.0",
"description": "Orama plugin to convert from jsonschema to Oramas schema format",
"keywords": [
"orama",
"jsonschema"
],
"license": "Apache-2.0",
"main": "./dist/index.js",
"type": "module",
"bugs": {
"url": "https://github.com/oramasearch/orama/issues"
},
"homepage": "https://github.com/oramasearch/orama#readme",
"repository": {
"type": "git",
"url": "git+https://github.com/oramasearch/orama.git"
},
"sideEffects": false,
"types": "./dist/index.d.ts",
"files": [
"dist"
],
"scripts": {
"build": "swc --delete-dir-on-start --extensions .ts,.cts -d dist src",
"postbuild": "tsc -p . --emitDeclarationOnly",
"lint": "eslint src --ext .js,.ts,.cts",
"test": "c8 -c test/config/c8.json tap --rcfile=test/config/tap.yml test/*.test.ts"
},
"dependencies": {
"@orama/orama": "workspace:*"
},
"publishConfig": {
"access": "public"
},
"lint-staged": {
"*.{ts, tsx}": "eslint ./src --cache --fix"
},
"devDependencies": {
"@swc/cli": "^0.1.59",
"@swc/core": "^1.3.27",
"@types/tap": "^15.0.7",
"c8": "^7.12.0",
"tap": "^16.3.0",
"tap-mocha-reporter": "^5.0.3",
"tsx": "^3.12.2",
"typescript": "^5.0.3"
}
}
38 changes: 38 additions & 0 deletions packages/plugin-jsonschema/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
type ScalarType = 'string' | 'number' | 'boolean'
type ValueType = ScalarType | `${ScalarType}[]` | TransformedSchema
interface TransformedSchema {
[key: string]: ValueType
}

function getOramaType(jsonSchema): ValueType {
switch (jsonSchema.type) {
case 'string':
case 'date':
return 'string'
case 'number':
case 'integer':
return 'number'
case 'boolean':
return 'boolean'
case 'array':
if (jsonSchema.items.type === 'object') throw new Error("Can't convert arrays of objects")
if (jsonSchema.items.type === 'array') throw new Error("Can't convert arrays of arrays")
return `${getOramaType(jsonSchema.items)}[]` as `${ScalarType}[]`
case 'object': {
const transformedSchema: TransformedSchema = {}
for (const key in jsonSchema.properties) {
transformedSchema[key] = getOramaType(jsonSchema.properties[key])
}
return transformedSchema
}
default:
throw new Error("Can't convert type " + jsonSchema.type)
}
}

export function fromJsonSchema(schema): TransformedSchema {
if (!schema || typeof schema !== 'object' || schema.type !== 'object') {
throw new Error('JSON schema must have top level type object')
}
return getOramaType(schema) as TransformedSchema
}
8 changes: 8 additions & 0 deletions packages/plugin-jsonschema/test/config/c8.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"check-coverage": true,
"reporter": ["text", "json"],
"branches": 80,
"functions": 80,
"lines": 80,
"statements": 80
}
8 changes: 8 additions & 0 deletions packages/plugin-jsonschema/test/config/tap.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
jobs: 5
timeout: 120
reporter: spec
coverage: false
node-arg:
- --loader=tsx
- --no-warnings=loader
131 changes: 131 additions & 0 deletions packages/plugin-jsonschema/test/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
import * as t from 'tap'
import { fromJsonSchema } from '../src/index.js'

t.test('convert sample schema', t => {
t.plan(1)
const jsonschema = {
type: 'object',
properties: {
resource_kind: { type: 'string' },
resource_uri: { type: 'string' },
resource_id: { type: 'string' },
data: {
type: 'object',
properties: {
filed: { type: 'boolean' },
barcode: { type: 'string' },
category: { type: 'string' },
date: { type: 'string' },
description: { type: 'string' },
description_values: {
type: 'object',
properties: {
made_up_date: { type: 'string' },
},
},
links: {
type: 'object',
properties: {
document_metadata: { type: 'string' },
self: { type: 'string' },
},
},
pages: { type: 'number' },
transaction_id: { type: 'string' },
type: { type: 'string' },
},
},
event: {
type: 'object',
properties: {
fields_changed: {
type: 'array',
items: { type: 'string' },
},
published_at: { type: 'string' },
type: { type: 'string' },
},
},
},
}

const oramaSchema = fromJsonSchema(jsonschema)

const expected = {
resource_kind: 'string',
resource_uri: 'string',
resource_id: 'string',
data: {
filed: 'boolean',
barcode: 'string',
category: 'string',
date: 'string',
description: 'string',
description_values: {
made_up_date: 'string',
},
links: {
document_metadata: 'string',
self: 'string',
},
pages: 'number',
transaction_id: 'string',
type: 'string',
},
event: {
fields_changed: 'string[]',
published_at: 'string',
type: 'string',
},
}

t.same(oramaSchema, expected)
})

t.test('should throw errors on invalid schemas', t => {
t.plan(4)

t.test('throw error for nested arrays', t => {
t.plan(1)
const jsonschema = {
type: 'object',
properties: {
coOrdinates: {
type: 'array',
items: { type: 'array', items: { type: 'number' } },
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This si supported as

{
  coOrdinates: 'number[]'
}

Or I missing something?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its a doubly nested array of arrays, data would look like this:

"coOrdinates": [
  [5, 7],
  [8, 3]
]

As far as I know orama doesn't support indexing that, so the converter to JSONschema would throw an error if it was attempted.

},
},
}
t.throws(() => fromJsonSchema(jsonschema), Error)
})
t.test('throw error for array of objects', t => {
t.plan(1)
const jsonschema = {
type: 'object',
properties: {
coOrdinates: {
type: 'array',
items: { type: 'object', properties: { x: { type: 'number' }, y: { type: 'number' } } },
},
},
}
t.throws(() => fromJsonSchema(jsonschema), Error)
})
t.test('throw error for non-object at top level', t => {
t.plan(1)
const jsonschema = {
type: 'string',
}
t.throws(() => fromJsonSchema(jsonschema), Error)
})
t.test('throw error for unknown type', t => {
t.plan(1)
const jsonschema = {
type: 'object',
properties: {
value: { type: 'unknown' },
},
}
t.throws(() => fromJsonSchema(jsonschema), Error)
})
})
19 changes: 19 additions & 0 deletions packages/plugin-jsonschema/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"compilerOptions": {
"allowJs": true,
"target": "ES5",
"module": "ESNext",
"outDir": "dist",
"jsx": "react",
"noImplicitAny": false,
"esModuleInterop": true,
"declaration": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true,
"resolveJsonModule": true,
"sourceMap": true,
"moduleResolution": "nodenext"
},
"include": ["src/*.ts", "src/**/*.ts", "src/*.tsx", "src/**/*.tsx"]
}
Loading