Skip to content

Commit

Permalink
Move plugin and schema.json out of internal repo
Browse files Browse the repository at this point in the history
This moves the schema.json out into the light and puts it with the rest
of the parsing and validating code where it belongs. Since they change
together and we should publish them together.

I removed checkfile related things from the plugin, we can decide what
to do about that. I also moved the JSON Schema checks to inside this
library instead of being in the plugin. We should add more unit testing
on this library to exercise and improve the JSON schema validation.

We can add some light XTP plugin tests to make sure that the plugin is
still working as behaving, but not going so far as to create a lot of
individual test cases that couple the wasm level tests to the
implementation details of things like features and error messages.
  • Loading branch information
bhelx committed Nov 26, 2024
1 parent e30bce4 commit 90c12b7
Show file tree
Hide file tree
Showing 9 changed files with 683 additions and 45 deletions.
16 changes: 8 additions & 8 deletions example-schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
# Learn more at https://docs.xtp.dylibso.com/docs/concepts/xtp-schema
version: v1-draft
exports:
CountVowels:
input:
type: string
contentType: text/plain; charset=utf-8
output:
$ref: "#/components/schemas/VowelReport"
contentType: application/json
countVowels:
input:
type: string
contentType: text/plain; charset=utf-8
output:
$ref: "#/components/schemas/VowelReport"
contentType: application/json
components:
schemas:
VowelReport:
Expand All @@ -25,4 +25,4 @@ components:
nullable: true
vowels:
type: string
description: The set of vowels used to get the count, e.g. "aAeEiIoOuU"
description: The set of vowels used to get the count, e.g. "aAeEiIoOuU"
41 changes: 41 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,11 @@
],
"keywords": [],
"author": "Dylibso, Inc.",
"license": "BSD-3-Clause"
"license": "BSD-3-Clause",
"dependencies": {
"ajv": "^8.17.1"
},
"overrides": {
"fast-uri": "github:dylibso/fast-uri#remove-unicode-flags-from-regex"
}
}
31 changes: 31 additions & 0 deletions plugin/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# XTP Schema Plugin

The XTP schema plugin is an wrapper around xtp-bindgen. It packages up
some of the functionality of the library into an Extism plugin.

It has 3 exports:

* validate_schema
* get_json_schema
* has_imports

## Usage

Compile:

```
xtp plugin build
```

Run:

```
cat ../example-schema.yaml | xtp plugin call dist/plugin.wasm validate_schema --stdin --wasi | jq
{
"valid": true,
//....
}
```


35 changes: 4 additions & 31 deletions plugin/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Ajv from "ajv";
import { SchemaValidationLog, SchemaValidationResult } from "./pdk";
import YAML from "js-yaml";
import JSON_SCHEMA from "BUILTIN_JSON_SCHEMA";
import { parse, XtpSchema, NormalizeError } from "@dylibso/xtp-bindgen";
import { parseYaml, XtpSchema, NormalizeError } from "@dylibso/xtp-bindgen";

/**
* Retrieves the JSON schema used for validation
Expand Down Expand Up @@ -38,36 +38,10 @@ export function has_importsImpl(input: string): string {
* @returns {SchemaValidationResult}
*/
export function validate_schemaImpl(input: string): SchemaValidationResult {
const yaml = YAML.load(input);

const ajv = new Ajv();
const validate = ajv.compile(JSON_SCHEMA);
if (!validate(yaml)) {
const errors = validate.errors!.map((error) => {
const err = {
path: error.instancePath ? error.instancePath : "#",
message: error.message!,
};

if (!err.path) {
err.path = "#";
}

return err;
});

return {
valid: false,
errors: errors,
warnings: [],
};
}

const schema = parseSchema(yaml);
const schema = parseSchema(input);

const warnings: SchemaValidationLog[] = [];

const version = (yaml as any).version;
const version = (input as any).version;
if (version && version.endsWith("-draft")) {
warnings.push({
message: `Version ${version} is a draft version and may be exposed to breaking changes until we publish the final version. See XTP docs for more info https://docs.xtp.dylibso.com/docs/concepts/xtp-schema#versioning`,
Expand All @@ -93,8 +67,7 @@ export function validate_schemaImpl(input: string): SchemaValidationResult {

function parseSchema(schema: any): XtpSchema | NormalizeError {
try {
const json = JSON.stringify(schema);
return parse(json);
return parseYaml(schema);
} catch (e) {
if (e instanceof NormalizeError) {
return e;
Expand Down
Loading

0 comments on commit 90c12b7

Please sign in to comment.