Skip to content

Implementing : Robust JSON Schema Validation#39

Open
Suvam-paul145 wants to merge 6 commits intobetterbugs:developfrom
Suvam-paul145:schema
Open

Implementing : Robust JSON Schema Validation#39
Suvam-paul145 wants to merge 6 commits intobetterbugs:developfrom
Suvam-paul145:schema

Conversation

@Suvam-paul145
Copy link

Problem Description

The existing JSON Validator tool relied on a simplified manual schema validation approach.
This caused several limitations:

  • Incomplete support for full JSON Schema specifications
  • No support for advanced keywords such as:
    • oneOf
    • $ref
    • format
    • exclusiveMinimum
    • enum
  • Validation stopped at the first error
  • Error messages were not standardized or fully descriptive
  • Limited support for nested object validation and reusable definitions

This reduced reliability and prevented professional-grade schema validation.


Solution Approach

1. Library Integration

Integrated the following libraries:

  • ajv (v8+) — Full JSON Schema validation engine
  • ajv-formats — Support for standard format validations (email, uri, etc.)

Configuration:

  • allErrors: true enabled to return complete validation feedback
  • Format validation enabled
  • Strict mode aligned with JSON Schema standards

2. Enhanced Validation Logic

Previous Approach

  • Manual schema property checks
  • Limited keyword handling
  • Basic type checking

New Approach

  • Used ajv.compile(schema) to generate a validation function
  • Executed validation via validate(data)
  • Processed Ajv error objects into readable messages

Error Path Transformation

Ajv provides instancePath in slash format:


Closes #34

- 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
Copilot AI review requested due to automatic review settings February 28, 2026 09:02
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 ajv and ajv-formats dependencies and updated the lockfile accordingly.
  • Updated jsonValidator.tsx to compile and run Ajv schema validation with allErrors: 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.

Comment on lines +7 to +8
const ajv = new Ajv({ allErrors: true, strict: false });
addFormats(ajv);
Copy link

Copilot AI Feb 28, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Comment on lines 182 to 197
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}`);
}
Copy link

Copilot AI Feb 28, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 uses AI. Check for mistakes.
@Suvam-paul145
Copy link
Author

@copilot open a new pull request to apply changes based on the comments in this thread

@SyedFahad7 SyedFahad7 self-requested a review February 28, 2026 14:47
@rishima17
Copy link
Collaborator

@Suvam-paul145 please raise PR against develop branch instead of main.

@Suvam-paul145 Suvam-paul145 changed the base branch from main to develop February 28, 2026 16:19
@Suvam-paul145
Copy link
Author

@rishima17 done

@Suvam-paul145
Copy link
Author

@SyedFahad7 please review my code

@Suvam-paul145
Copy link
Author

@rishima17 revie my code

@Suvam-paul145
Copy link
Author

@SyedFahad7 please review my code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add Robust JSON Schema Validation to JSON Validator

3 participants