Skip to content

[📝 Docs]: Create guide "How to validate data against a JSON schema" #954

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
1 task done
Tracked by #158
valeriahhdez opened this issue Sep 18, 2024 · 5 comments
Open
1 task done
Tracked by #158
Labels
📝 Documentation Indicates improvements or additions to documentation. Status: Available No one has claimed responsibility for resolving this issue.

Comments

@valeriahhdez
Copy link
Contributor

What Docs changes are you proposing?

We need a guide to teach users how to validate data against a JSON schema that is tool agnostic. Any ideas on this topic are welcome!

Code of Conduct

  • I agree to follow this project's Code of Conduct
@valeriahhdez valeriahhdez added 📝 Documentation Indicates improvements or additions to documentation. Status: Triage This is the initial status for an issue that requires triage. GSoD Google Season of Docs labels Sep 18, 2024
@valeriahhdez valeriahhdez self-assigned this Sep 18, 2024
@benjagm benjagm added Status: In Progress This issue is being worked on, and has someone assigned. and removed Status: Triage This is the initial status for an issue that requires triage. labels Sep 19, 2024
@rnd-debug
Copy link

Hello,
I am a simple user, but I was thinking : jwt.io does something similar in a user-friendly way. What if "teaching users how to validate a JSON against a schema in an agnostic way" consisted in providing the feature directly on the website ? In the case of jwt.io, for example, tokens are decoded and an error message (if needed) is directly displayed.
Or do you have something different in mind for this issue? (Maybe a guide about how to actually implement the validation ?)

PS: jwt.io also have a way of displaying a list of libraries that implement tokens en/decoding for different languages and different "levels" of implementation (https://jwt.io/libraries), but this is a different topic.

@valeriahhdez valeriahhdez removed Status: In Progress This issue is being worked on, and has someone assigned. GSoD Google Season of Docs labels Nov 6, 2024
@valeriahhdez valeriahhdez removed their assignment Nov 6, 2024
@valeriahhdez
Copy link
Contributor Author

Hello @rnd-debug,

Thank you for sharing these ideas; we'll take at look at the examples you provided. I updated the status of this issue because we haven't started it, yet. So, if you'd like to help us write this guide, you can claim this issue.

@valeriahhdez valeriahhdez moved this to Todo in JSON Schema Docs Nov 6, 2024
@valeriahhdez valeriahhdez added this to the Docs Release 6 milestone Nov 6, 2024
@valeriahhdez valeriahhdez added the Status: Available No one has claimed responsibility for resolving this issue. label Nov 7, 2024
@kapoorsaumitra
Copy link

hi @valeriahhdez

i'd like to contribute, can you please provide more context and details for this issue?

@harshita9104
Copy link

hello @valeriahhdez , @benjagm ,

Could you please assign me this issue,

I propose this solution:

  1. Progressive Type Validation
    Instead of validating everything at once, structure your schemas to validate data in layers:

{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "User Profile",
"type": "object",
"properties": {
"userId": {
"type": "string",
"pattern": "^[A-Za-z0-9-]+$",
"minLength": 3,
"maxLength": 50,
"description": "Alphanumeric user identifier"
}
}
}

This approach:

  • Validates basic type first
  • Then checks format constraints
  • Finally applies business rules
  1. Smart Defaults with ValidationCombine default values with validation to ensure data consistency:

{
"type": "object",
"properties": {
"userSettings": {
"type": "object",
"properties": {
"theme": {
"type": "string",
"enum": ["light", "dark", "system"],
"default": "system"
},
"notifications": {
"type": "boolean",
"default": true
}
}
}
}
}
3. Conditional Validation Pattern
Use if/then/else to create dynamic validation rules:

{
"type": "object",
"properties": {
"userType": {
"type": "string",
"enum": ["individual", "business"]
}
},
"if": {
"properties": {
"userType": { "const": "business" }
}
},
"then": {
"required": ["companyName", "vatNumber"]
},
"else": {
"required": ["firstName", "lastName"]
}
}

Best Practices

  1. Use Semantic Property Names
    Create schemas with clear, semantic property names that reflect their purpose:

{
"type": "object",
"properties": {
"emailAddress": {
"type": "string",
"format": "email"
},
"phoneNumber": {
"type": "string",
"pattern": "^\+?[1-9]\d{1,14}$"
}
}
}

  1. Custom Formats
    Define reusable custom formats for common validation patterns:

{
"type": "object",
"properties": {
"productCode": {
"$ref": "#/definitions/productCodeFormat"
}
},
"definitions": {
"productCodeFormat": {
"type": "string",
"pattern": "^[A-Z]{2}-[0-9]{6}$",
"description": "Product code in format XX-123456"
}
}
}

  1. Error Messages
    Include clear descriptions and examples to help users understand validation requirements:

{
"type": "object",
"properties": {
"password": {
"type": "string",
"minLength": 8,
"pattern": "^(?=.[A-Za-z])(?=.\d)[A-Za-z\d]{8,}$",
"description": "Password must be at least 8 characters and contain both letters and numbers",
"examples": ["securePass123"]
}
}
}

Advanced Patterns

  1. Composition Using allOf, anyOf, oneOf
    Combine multiple validation rules using logical operators:

{
"type": "object",
"properties": {
"data": {
"oneOf": [
{
"type": "object",
"properties": {
"type": { "const": "text" },
"content": { "type": "string" }
},
"required": ["type", "content"]
},
{
"type": "object",
"properties": {
"type": { "const": "number" },
"value": { "type": "number" }
},
"required": ["type", "value"]
}
]
}
}
}

  1. Schema Versioning
    Include version information in your schemas:

{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://example.com/schemas/user/v1.0.0",
"title": "User Schema v1.0.0",
"type": "object",
"properties": {
"schemaVersion": {
"type": "string",
"const": "1.0.0"
}
}
}

@oluwatobi2001
Copy link

Hi @valeriahhdez please can I be assigned to this issue? Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
📝 Documentation Indicates improvements or additions to documentation. Status: Available No one has claimed responsibility for resolving this issue.
Projects
Status: Todo
Development

No branches or pull requests

6 participants