Skip to content

Commit

Permalink
Merge pull request #130 from sellersindustry/103-ajv-support-on-vercel
Browse files Browse the repository at this point in the history
Added AJV Support
  • Loading branch information
SellersEvan authored May 23, 2024
2 parents cd18d75 + ab3112b commit 1a69c4e
Show file tree
Hide file tree
Showing 8 changed files with 177 additions and 4 deletions.
48 changes: 48 additions & 0 deletions docs/build/miscellaneous.mdx
Original file line number Diff line number Diff line change
@@ -1,6 +1,54 @@
# Miscellaneous


## Ajv JSON Schema Validator
[Ajv](https://ajv.js.org/), one of the best JSON schema validators, is
extremely useful in the development of backend APIs for data validation. But Ajv
is not supported by many edge environments, including Vercel, because of it's
usage of `eval`. However, the SherpaJS compiler will pre-compile standalone Ajv
schema, so schema validation can be done on the edge.

<br/>

### Example Static Schema
Start by creating a `schema.json` file containing the desired schema.
```json title="src/foo.schema.json"
{
"type": "object",
"properties": {
"foo": {
"type": "number"
}
},
"required": ["foo"],
"additionalProperties": false
}
```

Then import your schema (in any script), and write the schema in the `AJV`
function, provided by Sherpa Core.
```typescript title="routes/index.ts"
import { Request, Response, AJV } from "sherpa-core";
import schemaFoo from "../../src/foo.schema.json";
const validatorFoo = AJV(schemaFoo);

export function POST(request:Request) {
try {
if (!validatorFoo(request.body)) {
return Response.text(JSON.stringify(validatorFoo.errors));
}
return Response.text("OK");
} catch (e) {
console.log(e);
return Response.text(e.toString());
}
}
```


<br/>


## Environment Variables
Environment variables are a key part of configuring your SherpaJS application.
They allow you to set various configuration options and secrets without
Expand Down
76 changes: 72 additions & 4 deletions package-lock.json

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

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
},
"license": "ISC",
"dependencies": {
"@offen/esbuild-plugin-jsonschema": "^1.1.0",
"ajv": "^8.13.0",
"checksum": "^1.0.0",
"chokidar": "^3.6.0",
"colorette": "^2.0.20",
Expand Down
3 changes: 3 additions & 0 deletions src/compiler/utilities/tooling/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { typeValidation } from "./type-validation/index.js";
import { getEnvironmentVariables } from "./dot-env/index.js";
import { ExportLoaderModule, getExportedLoader } from "./exported-loader/index.js";
import { ExportedVariable, getExportedVariables } from "./exported-variables/index.js";
import jsonschemaPlugin from "@offen/esbuild-plugin-jsonschema";


export type { ExportLoaderModule, ExportedVariable };
Expand Down Expand Up @@ -65,6 +66,7 @@ export class Tooling {
...DEFAULT_ESBUILD_TARGET,
format: "cjs",
entryPoints: [filepath],
plugins: [jsonschemaPlugin()],
write: false
});

Expand All @@ -80,6 +82,7 @@ export class Tooling {
...DEFAULT_ESBUILD_TARGET,
...props.options?.developer?.bundler?.esbuild,
...props.esbuild,
plugins: [jsonschemaPlugin()],
stdin: {
contents: props.buffer,
resolveDir: props.resolve ? path.resolve(props.resolve) : undefined,
Expand Down
23 changes: 23 additions & 0 deletions src/native/ajv/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright (C) 2024 Sellers Industries, Inc.
* distributed under the MIT License
*
* author: Evan Sellers <[email protected]>
* date: Wed May 22 2024
* file: index.ts
* project: SherpaJS - Module Microservice Platform
* purpose: AJV Wrapper
*
*/


import { ValidateFunction, Schema, JSONSchemaType } from "ajv";


export function AJV<T=unknown>(schema:Schema|JSONSchemaType<T>):ValidateFunction<T> {
return schema as ValidateFunction<T>;
}


// The grace of the Lord Jesus Christ be with your spirit. Amen.
// - Philippians 4:23
2 changes: 2 additions & 0 deletions src/native/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
*/


import { AJV } from "./ajv/index.js";
import { Parameters } from "./parameters/index.js";
import { Headers } from "./headers/index.js";
import { Body, BodyType } from "./model.js";
Expand All @@ -20,6 +21,7 @@ import { CreateModuleInterface, Method, ModuleInterface } from "../compiler/mode


export {
AJV,
Headers,
Parameters,
Method,
Expand Down
17 changes: 17 additions & 0 deletions tests/endpoints/server/routes/ajv/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Request, Response, AJV } from "../../../../../index.js";
import schemaFoo from "../../src/foo.schema.json";
const validatorFoo = AJV(schemaFoo);


export function POST(request:Request) {
try {
if (!validatorFoo(request.body)) {
return Response.text(JSON.stringify(validatorFoo.errors));
}
return Response.text("OK");
} catch (e) {
console.log(e);
return Response.text(e.toString());
}
}

10 changes: 10 additions & 0 deletions tests/endpoints/server/src/foo.schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"type": "object",
"properties": {
"foo": {
"type": "number"
}
},
"required": ["foo"],
"additionalProperties": false
}

0 comments on commit 1a69c4e

Please sign in to comment.