-
-
Notifications
You must be signed in to change notification settings - Fork 297
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Nextra Plugin | ||
|
||
[![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) |
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" | ||
} | ||
} |
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 | ||
} |
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 | ||
} |
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 |
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' } }, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This si supported as {
coOrdinates: 'number[]'
} Or I missing something? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Its a doubly nested array of arrays, data would look like this:
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) | ||
}) | ||
}) |
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"] | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wrong title