diff --git a/app/components/developmentToolsComponent/jsonValidator.tsx b/app/components/developmentToolsComponent/jsonValidator.tsx index f83b21a..294ed50 100644 --- a/app/components/developmentToolsComponent/jsonValidator.tsx +++ b/app/components/developmentToolsComponent/jsonValidator.tsx @@ -1,6 +1,11 @@ "use client"; import React, { useState } from "react"; +import Ajv from "ajv"; +import addFormats from "ajv-formats"; + +const ajv = new Ajv({ allErrors: true, strict: false }); +addFormats(ajv); interface ValidationResult { isValid: boolean; @@ -178,47 +183,15 @@ const JsonValidator = () => { const json = JSON.parse(jsonString); const schema = JSON.parse(schemaString); - // Basic schema validation (simplified version) - if (schema.type) { - const actualType = Array.isArray(json) ? "array" : typeof json; - if (actualType !== schema.type) { - errors.push( - `Type mismatch: expected ${schema.type}, got ${actualType}` - ); - } - } + const validate = ajv.compile(schema); + const valid = validate(json); - if (schema.required && Array.isArray(schema.required)) { - schema.required.forEach((field: string) => { - if (!(field in json)) { - errors.push(`Required field missing: ${field}`); - } + if (!valid && validate.errors) { + validate.errors.forEach((err) => { + const path = err.instancePath ? err.instancePath.substring(1).replace(/\//g, ".") : "root"; + errors.push(`${path}: ${err.message}${err.params ? ` (${JSON.stringify(err.params)})` : ""}`); }); } - - if ( - schema.properties && - typeof json === "object" && - !Array.isArray(json) - ) { - Object.entries(schema.properties).forEach( - ([key, prop]: [string, any]) => { - if (key in json) { - const value = json[key]; - if (prop.type) { - const actualType = Array.isArray(value) - ? "array" - : typeof value; - if (actualType !== prop.type) { - errors.push( - `Property '${key}' type mismatch: expected ${prop.type}, got ${actualType}` - ); - } - } - } - } - ); - } } catch (error: any) { errors.push(`Schema validation error: ${error.message}`); } @@ -334,14 +307,71 @@ const JsonValidator = () => { const sampleSchemas = { user: JSON.stringify( { + $schema: "http://json-schema.org/draft-07/schema#", type: "object", - required: ["id", "name", "email"], + required: ["id", "name", "email", "address"], properties: { - id: { type: "number" }, - name: { type: "string" }, - email: { type: "string" }, - age: { type: "number" }, + id: { type: "integer", minimum: 1 }, + name: { type: "string", minLength: 2 }, + email: { type: "string", format: "email" }, + age: { type: "integer", minimum: 0, maximum: 120 }, isActive: { type: "boolean" }, + address: { + type: "object", + required: ["street", "city"], + properties: { + street: { type: "string" }, + city: { type: "string" }, + zipCode: { type: "string", pattern: "^[0-9]{5}(-[0-9]{4})?$" }, + }, + }, + tags: { + type: "array", + items: { type: "string" }, + uniqueItems: true, + }, + }, + }, + null, + 2 + ), + advanced: JSON.stringify( + { + $schema: "http://json-schema.org/draft-07/schema#", + title: "Advanced Schema", + definitions: { + address: { + type: "object", + properties: { + street: { type: "string" }, + city: { type: "string" }, + }, + }, + }, + type: "object", + properties: { + shipping_address: { $ref: "#/definitions/address" }, + billing_address: { $ref: "#/definitions/address" }, + contact: { + oneOf: [ + { + type: "object", + properties: { + type: { const: "email" }, + value: { type: "string", format: "email" }, + }, + required: ["type", "value"], + }, + { + type: "object", + properties: { + type: { const: "phone" }, + value: { type: "string", pattern: "^\\+[1-9]\\d{1,14}$" }, + }, + required: ["type", "value"], + }, + ], + }, }, }, null, @@ -352,10 +382,10 @@ const JsonValidator = () => { type: "object", required: ["id", "title", "price"], properties: { - id: { type: "string" }, + id: { type: "string", pattern: "^PRD-[0-9]{4}$" }, title: { type: "string" }, - price: { type: "number" }, - category: { type: "string" }, + price: { type: "number", exclusiveMinimum: 0 }, + category: { enum: ["electronics", "clothing", "home", "books"] }, inStock: { type: "boolean" }, }, }, @@ -369,327 +399,317 @@ const JsonValidator = () => {
- {/* Main Content */} -
- {/* Input Section */} -
-
-

Enter Value

-
- - - -
-
-