Files
plainleaf/common/syscalls/jsonschema.ts
T
Zef HemelandGitHub 80f9c14b96 Change how attribute indexing and completion works (#1061)
* Creation of separate aspring-page objects for pages linked to, but not created
* Show "No results" instead of broken markdown table for no query results
* Show schema validation errors
* Deno upgrade
* Adds config support to plugs (see examples)
* Moves all builtin schemas to plug config
* Adds core plug just for builtin schemas
* Changes how attributes are indexed and completed, now attempts to derive a JSON schema for ad hoc attributes
2024-08-24 12:35:09 +02:00

55 lines
1.2 KiB
TypeScript

import type { SysCallMapping } from "../../lib/plugos/system.ts";
import Ajv from "ajv";
const ajv = new Ajv();
ajv.addFormat("email", {
validate: (data: string) => {
// TODO: Implement email validation
return data.includes("@");
},
async: false,
});
ajv.addFormat("page-ref", {
validate: (data: string) => {
return data.startsWith("[[") && data.endsWith("]]");
},
async: false,
});
export function jsonschemaSyscalls(): SysCallMapping {
return {
"jsonschema.validateObject": (
_ctx,
schema: any,
object: any,
): undefined | string => {
try {
const validate = ajv.compile(schema);
if (validate(object)) {
return;
} else {
let text = ajv.errorsText(validate.errors);
text = text.replaceAll("/", ".");
text = text.replace(/^data[\.\s]/, "");
return text;
}
} catch (e) {
return e.message;
}
},
"jsonschema.validateSchema": (
_ctx,
schema: any,
): undefined | string => {
const valid = ajv.validateSchema(schema);
if (valid) {
return;
} else {
return ajv.errorsText(ajv.errors);
}
},
};
}