Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions js/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,21 @@
"@opentelemetry/api": "^1.9.0",
"@opentelemetry/context-async-hooks": "~1.25.0",
"@opentelemetry/core": "~1.25.0",
"@opentelemetry/exporter-jaeger": "^1.25.0",
"@opentelemetry/sdk-metrics": "~1.25.0",
"@opentelemetry/sdk-node": "^0.52.0",
"@opentelemetry/sdk-trace-base": "~1.25.0",
"@opentelemetry/exporter-jaeger": "^1.25.0",
"@types/json-schema": "^7.0.15",
"ajv": "^8.12.0",
"ajv-formats": "^3.0.1",
"async-mutex": "^0.5.0",
"body-parser": "^1.20.3",
"cors": "^2.8.5",
"dotprompt": "^1.1.1",
"express": "^4.21.0",
"get-port": "^5.1.0",
"json-schema": "^0.4.0",
"zod": "^3.23.8",
"zod-to-json-schema": "^3.22.4",
"dotprompt": "^1.1.1"
"zod-to-json-schema": "^3.22.4"
},
"devDependencies": {
"@types/express": "^4.17.21",
Expand All @@ -57,6 +56,7 @@
"typescript": "^4.9.0"
},
"optionalDependencies": {
"@cfworker/json-schema": "^4.1.1",
"@genkit-ai/firebase": "^1.16.1"
},
"types": "lib/index.d.ts",
Expand Down
7 changes: 6 additions & 1 deletion js/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,12 @@ export {
} from './flow.js';
export * from './plugin.js';
export * from './reflection.js';
export { defineJsonSchema, defineSchema, type JSONSchema } from './schema.js';
export {
defineJsonSchema,
defineSchema,
disableSchemaCodeGeneration,
type JSONSchema,
} from './schema.js';
export * from './telemetryTypes.js';
export * from './utils.js';

Expand Down
53 changes: 53 additions & 0 deletions js/core/src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,36 @@
* limitations under the License.
*/

import { Validator } from '@cfworker/json-schema';
import Ajv, { type ErrorObject, type JSONSchemaType } from 'ajv';
import addFormats from 'ajv-formats';
import { z } from 'zod';
import zodToJsonSchema from 'zod-to-json-schema';
import { GenkitError } from './error.js';
import { logger } from './logging.js';
import type { Registry } from './registry.js';
const ajv = new Ajv();
addFormats(ajv);

const SCHEMA_VALIDATION_MODE = 'schemaValidationMode' as const;

/**
* Disable schema code generation in runtime. Use this if your runtime
* environment restricts the use of `eval` or `new Function`, for e.g., in
* CloudFlare workers.
*/
export function disableSchemaCodeGeneration() {
logger.warn(
"It looks like you're trying to disable schema code generation. Please ensure that the '@cfworker/json-schema' package is installed: `npm i --save @cfworker/json-schema`"
);
global[SCHEMA_VALIDATION_MODE] = 'interpret';
}

/** Visible for testing */
export function resetSchemaCodeGeneration() {
global[SCHEMA_VALIDATION_MODE] = undefined;
}

export { z }; // provide a consistent zod to use throughout genkit

/**
Expand All @@ -32,6 +53,7 @@ export type JSONSchema = JSONSchemaType<any> | any;

const jsonSchemas = new WeakMap<z.ZodTypeAny, JSONSchema>();
const validators = new WeakMap<JSONSchema, ReturnType<typeof ajv.compile>>();
const cfWorkerValidators = new WeakMap<JSONSchema, Validator>();

/**
* Wrapper object for various ways schema can be provided.
Expand Down Expand Up @@ -97,6 +119,19 @@ function toErrorDetail(error: ErrorObject): ValidationErrorDetail {
};
}

function cfWorkerErrorToValidationErrorDetail(error: {
instanceLocation: string;
error: string;
}): ValidationErrorDetail {
const path = error.instanceLocation.startsWith('#/')
? error.instanceLocation.substring(2)
: '';
return {
path: path.replace(/\//g, '.') || '(root)',
message: error.error,
};
}

/**
* Validation response.
*/
Expand All @@ -115,6 +150,24 @@ export function validateSchema(
if (!toValidate) {
return { valid: true, schema: toValidate };
}
const validationMode = (global[SCHEMA_VALIDATION_MODE] ?? 'compile') as
| 'compile'
| 'interpret';

if (validationMode === 'interpret') {
let validator = cfWorkerValidators.get(toValidate);
if (!validator) {
validator = new Validator(toValidate);
cfWorkerValidators.set(toValidate, validator);
}
const result = validator.validate(data);
return {
valid: result.valid,
errors: result.errors?.map(cfWorkerErrorToValidationErrorDetail),
schema: toValidate,
};
}

const validator = validators.get(toValidate) || ajv.compile(toValidate);
const valid = validator(data) as boolean;
const errors = validator.errors?.map((e) => e);
Expand Down
29 changes: 28 additions & 1 deletion js/core/tests/schema_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,15 @@
* limitations under the License.
*/

import Ajv from 'ajv';
import * as assert from 'assert';
import { describe, it } from 'node:test';
import { describe, it, mock } from 'node:test';

import {
ValidationError,
disableSchemaCodeGeneration,
parseSchema,
resetSchemaCodeGeneration,
toJsonSchema,
validateSchema,
z,
Expand Down Expand Up @@ -162,3 +165,27 @@ describe('toJsonSchema', () => {
);
});
});

describe('disableSchemaCodeGeneration()', () => {
it('should validate using cfworker validator', () => {
const compileMock = mock.method(Ajv.prototype, 'compile');

disableSchemaCodeGeneration();
const result = validateSchema(
{ foo: 123 },
{
jsonSchema: {
type: 'object',
properties: { foo: { type: 'boolean' } },
},
}
);

assert.strictEqual(result.valid, false);
const errorAtFoo = result.errors?.find((e) => e.path === 'foo');
assert.ok(errorAtFoo, 'Should have error at foo');
assert.strictEqual(compileMock.mock.callCount(), 0);
compileMock.mock.restore();
resetSchemaCodeGeneration();
});
});
1 change: 1 addition & 0 deletions js/genkit/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ export {
UserFacingError,
defineJsonSchema,
defineSchema,
disableSchemaCodeGeneration,
getClientHeader,
getCurrentEnv,
getStreamingCallback,
Expand Down
Loading