-
Notifications
You must be signed in to change notification settings - Fork 70
feat: precompile cloudevent schema #471
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
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 |
---|---|---|
|
@@ -13,6 +13,7 @@ index.js | |
/bundles | ||
/dist | ||
/docs | ||
src/schema/v1.js | ||
|
||
# Runtime data | ||
pids | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,28 +3,17 @@ | |
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import Ajv, { Options } from "ajv"; | ||
import { ValidationError } from "./validation"; | ||
|
||
import { CloudEventV1 } from "./interfaces"; | ||
import { schemaV1 } from "./schemas"; | ||
import { Version } from "./cloudevent"; | ||
import validate from "../schema/v1"; | ||
|
||
const ajv = new Ajv({ extendRefs: true } as Options); | ||
|
||
// handle date-time format specially because a user could pass | ||
// Date().toString(), which is not spec compliant date-time format | ||
ajv.addFormat("js-date-time", function (dateTimeString) { | ||
const date = new Date(Date.parse(dateTimeString)); | ||
return date.toString() !== "Invalid Date"; | ||
}); | ||
|
||
const isValidAgainstSchemaV1 = ajv.compile(schemaV1); | ||
|
||
export function validateCloudEvent<T>(event: CloudEventV1<T>): boolean { | ||
if (event.specversion === Version.V1) { | ||
if (!isValidAgainstSchemaV1(event)) { | ||
throw new ValidationError("invalid payload", isValidAgainstSchemaV1.errors); | ||
if (!validate(event)) { | ||
throw new ValidationError("invalid payload", (validate as any).errors); | ||
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.
I'm seeing function validate(data: any, { instancePath, parentData, parentDataProperty, rootData }?: {
instancePath?: string | undefined;
parentData: any;
parentDataProperty: any;
rootData?: any;
}): boolean 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. The function that AJV generates has an function validate20(data, { instancePath = "", parentData, parentDataProperty, rootData = data } = {}) {
let vErrors = null;
let errors = 0;
if (errors === 0) {
if (data && typeof data == "object" && !Array.isArray(data)) {
let missing0;
if (
(data.id === undefined && (missing0 = "id")) ||
(data.source === undefined && (missing0 = "source")) ||
(data.specversion === undefined && (missing0 = "specversion")) ||
(data.type === undefined && (missing0 = "type"))
) {
validate20.errors = [
{
instancePath,
schemaPath: "#/required",
keyword: "required",
params: { missingProperty: missing0 },
message: `must have required property '${ missing0 }'`,
},
];
return false;
} |
||
} | ||
} else { | ||
return false; | ||
|
Uh oh!
There was an error while loading. Please reload this page.