Implementing : Robust JSON Schema Validation#39
Implementing : Robust JSON Schema Validation#39Suvam-paul145 wants to merge 6 commits intobetterbugs:developfrom
Conversation
- Created passwordStrengthMeter.tsx component with advanced password analysis - Uses zxcvbn library for realistic entropy-based strength assessment - Displays color-coded strength meter (Very Weak to Very Strong) - Shows detailed crack time estimates for different attack scenarios - Provides warnings and suggestions for password improvement - Includes visibility toggle, copy, and clear functionality - Added comprehensive component configuration to developmentToolsConstant.tsx - Registered component and routes in constants.tsx - Added zxcvbn and @types/zxcvbn dependencies to package.json - Full TypeScript typing with proper null checks - Matches project styling and UI patterns Closes betterbugs#19
This reverts commit 82081c7.
There was a problem hiding this comment.
Pull request overview
This PR upgrades the JSON Validator tool to use Ajv for full JSON Schema validation (including advanced keywords and multi-error reporting), replacing the prior manual/partial schema checks.
Changes:
- Added
ajvandajv-formatsdependencies and updated the lockfile accordingly. - Updated
jsonValidator.tsxto compile and run Ajv schema validation withallErrors: true, formatting Ajv errors into path-based messages. - Expanded sample schemas to demonstrate advanced JSON Schema features (
format,$ref,oneOf,exclusiveMinimum,enum, nested objects, etc.).
Reviewed changes
Copilot reviewed 2 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| package.json | Adds Ajv + ajv-formats dependencies needed for robust JSON Schema validation. |
| package-lock.json | Updates dependency graph for Ajv v8 and related packages. |
| app/components/developmentToolsComponent/jsonValidator.tsx | Replaces manual schema checks with Ajv-based validation and enhances sample schemas/UI behavior. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| const ajv = new Ajv({ allErrors: true, strict: false }); | ||
| addFormats(ajv); |
There was a problem hiding this comment.
Ajv is instantiated with strict: false, which contradicts the PR description (“Strict mode aligned with JSON Schema standards”) and also makes it easier for invalid/typo’d schema keywords to slip through silently. Consider enabling strict mode (or using strict: "log" if you want non-blocking feedback) and documenting any intentional relaxations.
| try { | ||
| 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}`); | ||
| } |
There was a problem hiding this comment.
The catch-all error message Schema validation error: ... is also used when JSON parsing fails for either the data or the schema, which can be misleading to users (e.g., invalid JSON input will be reported as a schema error). Consider splitting parsing of jsonString vs schemaString into separate try/catch blocks (or validating JSON first) so the error message clearly indicates whether the data JSON or the schema JSON is invalid.
|
@copilot open a new pull request to apply changes based on the comments in this thread |
|
@Suvam-paul145 please raise PR against develop branch instead of main. |
|
@rishima17 done |
|
@SyedFahad7 please review my code |
|
@rishima17 revie my code |
|
@SyedFahad7 please review my code |
Problem Description
The existing JSON Validator tool relied on a simplified manual schema validation approach.
This caused several limitations:
oneOf$refformatexclusiveMinimumenumThis reduced reliability and prevented professional-grade schema validation.
Solution Approach
1. Library Integration
Integrated the following libraries:
Configuration:
allErrors: trueenabled to return complete validation feedback2. Enhanced Validation Logic
Previous Approach
New Approach
ajv.compile(schema)to generate a validation functionvalidate(data)Error Path Transformation
Ajv provides
instancePathin slash format:Closes #34