### Install Cabidela package Source: https://github.com/cloudflare/cabidela/blob/main/README.md Install the package via npm. ```bash npm install @cloudflare/cabidela --save ``` -------------------------------- ### Cabidela Initialization and Usage Source: https://github.com/cloudflare/cabidela/blob/main/README.md Demonstrates how to install, import, and use Cabidela to validate a JSON payload against a schema. ```APIDOC ## Installation ```bash npm install @cloudflare/cabidela --save ``` ## Import ```ts import { Cabidela } from "@cloudflare/cabidela"; ``` ## Basic Usage ```ts let schema: any = { type: "object", properties: { prompt: { type: "string", minLength: 1, maxLength: 131072, description: "The input text prompt for the model to generate a response.", }, num_steps: { type: "number", minimum: 0, maximum: 20, description: "Increases the likelihood of the model introducing new topics.", }, }, required: ["prompt"], }; const cabidela = new Cabidela(schema); cabidela.validate({ prompt: "Tell me a joke", num_steps: 5, }); ``` ``` -------------------------------- ### Define oneOf schema with defaults Source: https://github.com/cloudflare/cabidela/blob/main/README.md Example schema demonstrating how applyDefaults interacts with oneOf conditions. ```javascript { type: "object", oneOf: [ { type: "object", properties: { sun: { type: "number", default: 9000, }, moon: { type: "number", default: 9000, }, }, required: ["sun"], }, { type: "object", properties: { sun: { type: "number", default: 9000, }, moon: { type: "number", default: 9000, }, }, required: ["moon"], }, ], }; ``` -------------------------------- ### Cabidela API - New Instance Source: https://github.com/cloudflare/cabidela/blob/main/README.md Details on how to create a new instance of the Cabidela validator with various configuration options. ```APIDOC ## New instance `const cabidela = new Cabidela(schema: any, options?: CabidelaOptions)` Cabidela takes a JSON-Schema and optional configuration flags: - `applyDefaults`: boolean - If true, the validator will apply default values to the input object. Default is false. - `errorMessages`: boolean - If true, the validator will use custom `errorMessage` messages from the schema. Default is false. - `fullErrors`: boolean - If true, the validator will be more verbose when throwing errors for complex schemas (example: anyOf, oneOf's), set to false for shorter exceptions. Default is true. - `useMerge`: boolean - Set to true if you want to use the `$merge` keyword. Default is false. See below for more information. - `subSchemas`: any[] - An optional array of sub-schemas that can be used with `$id` and `$ref`. See below for more information. Returns a validation object. You can change the schema at any time by calling `cabidela.setSchema(schema: any)`. You can change the options at any time by calling `cabidela.setOptions(options: CabidelaOptions)`. ``` -------------------------------- ### Initialize and validate schema Source: https://github.com/cloudflare/cabidela/blob/main/README.md Define a schema, instantiate the validator, and validate an object. ```ts let schema: any = { type: "object", properties: { prompt: { type: "string", minLength: 1, maxLength: 131072, description: "The input text prompt for the model to generate a response.", }, num_steps: { type: "number", minimum: 0, maximum: 20, description: "Increases the likelihood of the model introducing new topics.", }, }, required: ["prompt"], }; const cabidela = new Cabidela(schema); cabidela.validate({ prompt: "Tell me a joke", num_steps: 5, }); ``` -------------------------------- ### Run All Cabidela Tests Source: https://github.com/cloudflare/cabidela/blob/main/README.md Execute all available tests, including those with Ajv, to ensure comprehensive validation and specification compliance. Use `npm run test-all`. ```bash npm run test-all ``` -------------------------------- ### Import Cabidela Source: https://github.com/cloudflare/cabidela/blob/main/README.md Import the Cabidela class into your project. ```ts import { Cabidela } from "@cloudflare/cabidela"; ``` -------------------------------- ### Run Cabidela Tests with Ajv Source: https://github.com/cloudflare/cabidela/blob/main/README.md Compare Cabidela's validation results with Ajv by running tests using `npm run test-ajv`. This helps in verifying the interpretation of the JSON Schema specification. ```bash npm run test-ajv ``` -------------------------------- ### Cabidela API - oneOf Defaults Source: https://github.com/cloudflare/cabidela/blob/main/README.md Explains how `applyDefaults` interacts with `oneOf` schemas, applying defaults from the matching sub-schema. ```APIDOC ## oneOf defaults Using `applyDefaults` with `oneOf` will one apply the default value of the sub-schema that matches the condition. For example, using this schema: ```javascript { type: "object", oneOf: [ { type: "object", properties: { sun: { type: "number", default: 9000, }, moon: { type: "number", default: 9000, }, }, required: ["sun"], }, { type: "object", properties: { sun: { type: "number", default: 9000, }, moon: { type: "number", default: 9000, }, }, required: ["moon"], }, ], }; ``` - The payload `{ sun: 10}` will be modified to `{ sun: 10, moon: 9000 }`. - The payload `{ moon: 10}` will be modified to `{ sun: 9000, moon: 10 }`. - The payload `{ saturn: 10}` will throw an error because no condition is met. ``` -------------------------------- ### Define and Validate Complex Schemas with $ref and $defs Source: https://github.com/cloudflare/cabidela/blob/main/README.md Use $id, $ref, and $defs to structure complex schemas by referencing reusable parts defined in separate schemas. Ensure subSchemas are provided when initializing Cabidela. ```javascript import { Cabidela } from "@cloudflare/cabidela"; const schema = { $id: "http://example.com/schemas/main", type: "object", properties: { name: { type: "string" }, contacts: { $ref: "customer#/contacts" }, address: { $ref: "customer#/address" }, balance: { $ref: "$defs#/balance" }, }, required: ["name", "contacts", "address"], "$defs": { "balance": { type: "object", prope properties: { currency: { type: "string" }, amount: { type: "number" }, }, } } }; const contactSchema = { $id: "http://example.com/schemas/customer", contacts: { type: "object", properties: { email: { type: "string" }, phone: { type: "string" }, }, required: ["email", "phone"], }, address: { type: "object", properties: { street: { type: "string" }, city: { type: "string" }, zip: { type: "string" }, country: { type: "string" }, }, required: ["street", "city", "zip", "country"], }, }; const cabidela = new Cabidela(schema, { subSchemas: [contactSchema] }); cabidela.validate({ name: "John", contacts: { email: "john@example.com", phone: "+123456789", }, address: { street: "123 Main St", city: "San Francisco", zip: "94105", country: "USA", }, }); ``` -------------------------------- ### Run Cabidela Tests Source: https://github.com/cloudflare/cabidela/blob/main/README.md Execute internal tests and JSON Schema specification compliance checks using vitest. Run all tests with `npm run test`. ```bash npm run test ``` -------------------------------- ### Reuse Schemas with $ref, $id, and $defs Source: https://context7.com/cloudflare/cabidela/llms.txt Structure modular schemas by defining reusable components in $defs and referencing them via $ref. ```typescript import { Cabidela } from "@cloudflare/cabidela"; // Main schema with references to sub-schemas and $defs const mainSchema = { $id: "http://example.com/schemas/main", type: "object", properties: { name: { type: "string" }, contacts: { $ref: "customer#/contacts" }, address: { $ref: "customer#/address" }, balance: { $ref: "$defs#/balance" }, }, required: ["name", "contacts", "address"], $defs: { balance: { type: "object", properties: { currency: { type: "string" }, amount: { type: "number" }, }, required: ["currency", "amount"], }, }, }; // External sub-schema const customerSchema = { $id: "http://example.com/schemas/customer", contacts: { type: "object", properties: { email: { type: "string" }, phone: { type: "string" }, }, required: ["email", "phone"], }, address: { type: "object", properties: { street: { type: "string" }, city: { type: "string" }, zip: { type: "string" }, country: { type: "string" }, }, required: ["street", "city", "zip", "country"], }, }; // Create validator with sub-schemas const cabidela = new Cabidela(mainSchema, { subSchemas: [customerSchema] }); // Validate a complete payload cabidela.validate({ name: "John Doe", contacts: { email: "john@example.com", phone: "+1-555-123-4567", }, address: { street: "123 Main St", city: "San Francisco", zip: "94105", country: "USA", }, balance: { currency: "USD", amount: 1000.50, }, }); // You can also add schemas dynamically const anotherCabidela = new Cabidela(mainSchema); anotherCabidela.addSchema(customerSchema); ``` -------------------------------- ### Instantiate Cabidela Validator Source: https://context7.com/cloudflare/cabidela/llms.txt Initialize a validator instance with a JSON schema and optional configuration settings. ```typescript import { Cabidela } from "@cloudflare/cabidela"; const schema = { type: "object", properties: { prompt: { type: "string", minLength: 1, maxLength: 131072, description: "The input text prompt for the model to generate a response.", }, num_steps: { type: "number", minimum: 0, maximum: 20, description: "Number of inference steps.", }, }, required: ["prompt"], }; // Basic instantiation const cabidela = new Cabidela(schema); // With options const cabidelaWithOptions = new Cabidela(schema, { applyDefaults: true, // Apply default values to payload errorMessages: true, // Use custom errorMessage from schema fullErrors: true, // Verbose errors for complex schemas useMerge: false, // Enable $merge keyword support subSchemas: [], // Array of sub-schemas for $ref resolution }); ``` -------------------------------- ### Cabidela API - Modifying the Payload with Defaults Source: https://github.com/cloudflare/cabidela/blob/main/README.md Illustrates how the `applyDefaults` option modifies the input object by applying default values defined in the schema. ```APIDOC ## Modifying the payload Some options, like `applyDefaults`, will modify the input object. ```js const schema = { type: "object", properties: { prompt: { type: "string", }, num_steps: { type: "number", default: 10, }, }, }; const cabidela = new Cabidela(schema, { applyDefaults: true }); const payload = { prompt: "Tell me a joke", }); cabidela.validate(payload); console.log(payload); // { // prompt: 'Tell me a joke', // num_steps: 10 // } ``` ``` -------------------------------- ### Cabidela API - Validate Payload Source: https://github.com/cloudflare/cabidela/blob/main/README.md Explains how to use the `validate` method to check a payload against the current schema and how to handle potential errors. ```APIDOC ## Validate payload Call `cabidela.validate(payload: any)` to validate your payload. Returns truth if the payload is valid, throws an error otherwise. ```js const payload = { messages: [ { role: "system", content: "You're a helpful assistant" }, { role: "user", content: "What is Cloudflare?" }, ], }; try { cabidela.validate(payload); console.log("Payload is valid"); } catch (e) { console.error(e); } ``` ``` -------------------------------- ### Combine Schemas using $merge Source: https://github.com/cloudflare/cabidela/blob/main/README.md Use the $merge keyword to combine schemas, extending a source schema with properties from another. Initialize Cabidela with `useMerge: true` to enable this functionality. ```json { "$merge": { "source": { "type": "object", "properties": { "p": { "type": "string" } }, "additionalProperties": false }, "with": { "properties": { "q": { "type": "number" } } } } } ``` ```json { "type": "object", "properties": { "p": { "type": "string" } }, "q": { "type": "number" } }, "additionalProperties": false } ``` -------------------------------- ### Modify payload with defaults Source: https://github.com/cloudflare/cabidela/blob/main/README.md Enable applyDefaults to automatically populate missing fields with default values defined in the schema. ```js const schema = { type: "object", properties: { prompt: { type: "string", }, num_steps: { type: "number", default: 10, }, }, }; const cabidela = new Cabidela(schema, { applyDefaults: true }); const payload = { prompt: "Tell me a joke", }); cabidela.validate(payload); console.log(payload); // { // prompt: 'Tell me a joke', // num_steps: 10 // } ``` -------------------------------- ### Compose Schemas with allOf, anyOf, and oneOf Source: https://context7.com/cloudflare/cabidela/llms.txt Use composition keywords to define complex validation logic requiring data to match multiple, any, or exactly one subschema. ```typescript import { Cabidela } from "@cloudflare/cabidela"; // allOf: Data must be valid against ALL subschemas const allOfSchema = { allOf: [ { type: "string" }, { maxLength: 5 }, ], }; const allOfValidator = new Cabidela(allOfSchema); allOfValidator.validate("short"); // Valid // allOfValidator.validate("too long"); // Throws error // anyOf: Data must be valid against ANY (one or more) subschemas const anyOfSchema = { anyOf: [ { type: "string", maxLength: 5 }, { type: "number", minimum: 0 }, ], }; const anyOfValidator = new Cabidela(anyOfSchema); anyOfValidator.validate("hi"); // Valid (matches first) anyOfValidator.validate(42); // Valid (matches second) // anyOfValidator.validate(-5); // Throws error (matches neither) // oneOf: Data must be valid against EXACTLY ONE subschema const oneOfSchema = { oneOf: [ { type: "number", multipleOf: 5 }, { type: "number", multipleOf: 3 }, ], }; const oneOfValidator = new Cabidela(oneOfSchema); oneOfValidator.validate(10); // Valid (only multiple of 5) oneOfValidator.validate(9); // Valid (only multiple of 3) // oneOfValidator.validate(15); // Throws error (multiple of both 5 and 3) // oneOfValidator.validate(7); // Throws error (multiple of neither) ``` -------------------------------- ### Validate payload with error handling Source: https://github.com/cloudflare/cabidela/blob/main/README.md Use a try-catch block to handle validation failures, as Cabidela throws errors when conditions are not met. ```js const payload = { messages: [ { role: "system", content: "You're a helpful assistant" }, { role: "user", content: "What is Cloudflare?" }, ], }; try { cabidela.validate(payload); console.log("Payload is valid"); } catch (e) { console.error(e); } ``` -------------------------------- ### Extend Schemas with $merge Source: https://context7.com/cloudflare/cabidela/llms.txt Use the $merge keyword to combine and extend schema properties, optionally enabling it via the useMerge configuration option. ```typescript import { Cabidela } from "@cloudflare/cabidela"; const schema = { $merge: { source: { type: "object", properties: { name: { type: "string" } }, additionalProperties: false, }, with: { properties: { age: { type: "number" } }, }, }, }; const cabidela = new Cabidela(schema, { useMerge: true }); // The merged schema becomes: // { // type: "object", // properties: { // name: { type: "string" }, // age: { type: "number" } // }, // additionalProperties: false // } cabidela.validate({ name: "Alice", age: 30 }); // Valid // $merge can be combined with $ref and $defs const schemaWithRefs = { $merge: { source: { type: "object", properties: { prompt: { type: "string" } }, }, with: { properties: { maxLength: { $ref: "$defs#/max_tokens" }, }, }, }, $defs: { max_tokens: 250, }, }; const refCabidela = new Cabidela(schemaWithRefs, { useMerge: true }); console.log(refCabidela.getSchema()); // Output: { type: "object", properties: { prompt: { type: "string" }, maxLength: 250 } } ``` -------------------------------- ### Enable $merge Keyword in Cabidela Source: https://github.com/cloudflare/cabidela/blob/main/README.md To utilize the $merge keyword for combining schemas, set the `useMerge` flag to `true` when creating a new Cabidela instance. ```javascript new Cabidela(schema, { useMerge: true }); ``` -------------------------------- ### Update Schema and Options at Runtime Source: https://context7.com/cloudflare/cabidela/llms.txt Modify the validator configuration dynamically using setSchema and setOptions methods. ```typescript import { Cabidela } from "@cloudflare/cabidela"; const initialSchema = { type: "object", properties: { name: { type: "string" }, }, }; const cabidela = new Cabidela(initialSchema); // Update schema const newSchema = { type: "object", properties: { name: { type: "string" }, email: { type: "string" }, }, required: ["name", "email"], }; cabidela.setSchema(newSchema); cabidela.validate({ name: "John", email: "john@example.com" }); // Valid // Update options cabidela.setOptions({ applyDefaults: true, errorMessages: true }); // Get current schema const currentSchema = cabidela.getSchema(); console.log(currentSchema); ``` -------------------------------- ### Configure Custom Error Messages Source: https://context7.com/cloudflare/cabidela/llms.txt Use the errorMessage property within a schema to provide user-friendly feedback. Ensure errorMessages is set to true in the Cabidela options. ```typescript import { Cabidela } from "@cloudflare/cabidela"; const schema = { type: "object", properties: { prompt: { type: "string" }, }, required: ["prompt"], errorMessage: "A prompt is required to generate a response", }; const cabidela = new Cabidela(schema, { errorMessages: true }); try { cabidela.validate({ notPrompt: "value" }); } catch (e) { console.error(e.message); // Output: "A prompt is required to generate a response" } // Custom errors work with nested schemas and oneOf const chatSchema = { type: "object", oneOf: [ { properties: { prompt: { type: "string", minLength: 1 }, }, required: ["prompt"], errorMessage: "prompt is required", }, { properties: { messages: { type: "array", items: { type: "object", properties: { role: { type: "string" }, content: { type: "string" }, }, required: ["role", "content"], errorMessage: "messages need both role and content", }, }, }, required: ["messages"], errorMessage: "messages array is required", }, ], }; const chatCabidela = new Cabidela(chatSchema, { errorMessages: true }); try { chatCabidela.validate({ invalid: "data" }); } catch (e) { console.error(e.message); // Output: "oneOf at '.' not met, 0 matches: prompt is required, messages array is required" } ``` -------------------------------- ### Define Custom Error Messages for Validation Source: https://github.com/cloudflare/cabidela/blob/main/README.md Enable custom error messages by setting `errorMessages: true` during Cabidela instantiation. Use the `errorMessage` property within the schema to specify custom messages for validation failures. ```javascript const schema = { type: "object", properties: { prompt: { type: "string", }, }, required: ["prompt"], errorMessage: "prompt required", }; const cabidela = new Cabidela(schema, { errorMessages: true }); const payload = { missing: "prompt", }); cabidela.validate(payload); // throws "Error: prompt required" ``` -------------------------------- ### Apply Default Values Source: https://context7.com/cloudflare/cabidela/llms.txt Enable automatic population of default values from the schema into the payload during validation. ```typescript import { Cabidela } from "@cloudflare/cabidela"; const schema = { type: "object", properties: { prompt: { type: "string" }, temperature: { type: "number", default: 0.7 }, max_tokens: { type: "integer", default: 256 }, }, required: ["prompt"], }; const cabidela = new Cabidela(schema, { applyDefaults: true }); const payload = { prompt: "Tell me a joke", }; cabidela.validate(payload); console.log(payload); // Output: { prompt: "Tell me a joke", temperature: 0.7, max_tokens: 256 } // Defaults also work with arrays const arraySchema = { type: "object", properties: { messages: { type: "array", items: { type: "object", properties: { role: { type: "string", default: "user" }, content: { type: "string" }, }, required: ["role", "content"], }, }, }, }; const arrayCabidela = new Cabidela(arraySchema, { applyDefaults: true }); const arrayPayload = { messages: [ { content: "Hello" }, { role: "assistant", content: "Hi there" }, ], }; arrayCabidela.validate(arrayPayload); console.log(arrayPayload.messages[0].role); // Output: "user" console.log(arrayPayload.messages[1].role); // Output: "assistant" ``` -------------------------------- ### Validate Payloads Source: https://context7.com/cloudflare/cabidela/llms.txt Perform validation against a schema, which returns true on success or throws an error on failure. ```typescript import { Cabidela } from "@cloudflare/cabidela"; const schema = { type: "object", properties: { messages: { type: "array", items: { type: "object", properties: { role: { type: "string" }, content: { type: "string" }, }, required: ["role", "content"], }, }, }, required: ["messages"], }; const cabidela = new Cabidela(schema); const payload = { messages: [ { role: "system", content: "You're a helpful assistant" }, { role: "user", content: "What is Cloudflare?" }, ], }; try { cabidela.validate(payload); console.log("Payload is valid"); } catch (e) { console.error("Validation failed:", e.message); // Example error: "required properties at '.' are 'messages'" } ``` -------------------------------- ### Perform Type Validation Source: https://context7.com/cloudflare/cabidela/llms.txt Validate various data types including strings, numbers, arrays, and objects using standard JSON Schema constraints. ```typescript import { Cabidela } from "@cloudflare/cabidela"; // String validation with length constraints and patterns const stringSchema = { type: "object", properties: { username: { type: "string", minLength: 3, maxLength: 20 }, email: { type: "string", pattern: "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$" }, }, }; const stringValidator = new Cabidela(stringSchema); stringValidator.validate({ username: "john_doe", email: "john@example.com" }); // Number validation with range constraints const numberSchema = { type: "object", properties: { temperature: { type: "number", minimum: 0, maximum: 1 }, count: { type: "integer", minimum: 1, maximum: 100 }, score: { type: "number", exclusiveMinimum: 0, exclusiveMaximum: 10 }, quantity: { type: "integer", multipleOf: 5 }, }, }; const numberValidator = new Cabidela(numberSchema); numberValidator.validate({ temperature: 0.7, count: 50, score: 5.5, quantity: 25 }); // Array validation const arraySchema = { type: "object", properties: { tags: { type: "array", items: { type: "string" }, }, scores: { type: "array", items: { type: "number", minimum: 0, maximum: 100 }, }, }, }; const arrayValidator = new Cabidela(arraySchema); arrayValidator.validate({ tags: ["ai", "ml", "nlp"], scores: [85, 92, 78] }); // Enum and const validation const enumSchema = { type: "object", properties: { status: { enum: ["pending", "active", "completed"] }, version: { const: "1.0" }, }, }; const enumValidator = new Cabidela(enumSchema); enumValidator.validate({ status: "active", version: "1.0" }); // Object with additionalProperties constraint const strictObjectSchema = { type: "object", properties: { name: { type: "string" }, age: { type: "number" }, }, additionalProperties: false, }; const strictValidator = new Cabidela(strictObjectSchema); strictValidator.validate({ name: "Alice", age: 30 }); // Valid // strictValidator.validate({ name: "Alice", age: 30, extra: "field" }); // Throws error ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.