How to Validate the Exercises Dataset Using JSON Schema
The exercises dataset provides a formal JSON Schema at data/exercises.schema.json that follows Draft 2020-12, allowing you to validate exercise records against strict type constraints, required multilingual fields, and URI formats before processing.
The hasaneyldrm/exercises-dataset repository ships with a machine-readable schema that defines the structure of every exercise record. By validating your JSON against this schema, you can catch missing fields, type mismatches, and malformed URLs before importing data into your application. This guide demonstrates how to validate the exercises dataset using JSON Schema validators in Python, Node.js, Go, and command-line tools.
Understanding the JSON Schema Structure
The schema file data/exercises.schema.json implements the JSON Schema Draft 2020-12 specification. It defines strict constraints for exercise records stored in data/exercises.json.
Key field definitions include:
- id: String representing the numeric identifier
- name, category, body_part, equipment, muscle_group, target: String values
- media_id, image, gif_url: Strings with
format: urivalidation - instructions: Object requiring exactly nine language keys (
en,es,it,tr,ru,zh,hi,pl,ko), each containing a string instruction block - instruction_steps: Object where each language entry contains an array of string steps
- secondary_muscles: Array of strings
- created_at: ISO-8601 date-time string with
format: date-timeconstraint
Validating in Python
The jsonschema library provides a straightforward way to validate each exercise record against the schema.
import json
from jsonschema import validate, ValidationError
# Load the schema
with open("data/exercises.schema.json", "r", encoding="utf-8") as f:
schema = json.load(f)
# Load the dataset (or any custom JSON)
with open("data/exercises.json", "r", encoding="utf-8") as f:
exercises = json.load(f)
# Validate each record
errors = []
for idx, ex in enumerate(exercises, start=1):
try:
validate(instance=ex, schema=schema)
except ValidationError as e:
errors.append(f"Exercise #{idx} ({ex.get('id')}): {e.message}")
if errors:
print("Validation failed:")
for err in errors:
print(err)
else:
print("All exercises validated successfully")
Validating in Node.js
Use AJV (Another JSON Schema Validator) to compile the schema and validate the dataset.
const Ajv = require("ajv");
const fs = require("fs");
// Load schema and data
const schema = JSON.parse(fs.readFileSync("data/exercises.schema.json", "utf-8"));
const exercises = JSON.parse(fs.readFileSync("data/exercises.json", "utf-8"));
const ajv = new Ajv({ allErrors: true });
const validate = ajv.compile(schema);
let hasError = false;
exercises.forEach((ex, i) => {
const valid = validate(ex);
if (!valid) {
hasError = true;
console.error(`Exercise #${i + 1} (id=${ex.id}) validation errors:`);
console.error(validate.errors);
}
});
if (!hasError) console.log("All exercises passed AJV validation");
Validating via Command Line
The ajv-cli tool enables quick validation without writing code.
# Install globally (once)
npm i -g ajv-cli
# Validate the whole dataset
ajv validate -s data/exercises.schema.json -d data/exercises.json
Validating in Go
The github.com/santhosh-tekuri/jsonschema/v5 package provides Draft 2020-12 support for Go applications.
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"github.com/santhosh-tekuri/jsonschema/v5"
)
func main() {
// Compile the schema
compiler := jsonschema.NewCompiler()
if err := compiler.AddResource("schema.json",
// Load from file
// (you could also embed the schema)
); err != nil {
log.Fatal(err)
}
schema, err := compiler.Compile("schema.json")
if err != nil {
log.Fatal(err)
}
// Load the dataset
data, _ := ioutil.ReadFile("data/exercises.json")
var exercises []interface{}
if err := json.Unmarshal(data, &exercises); err != nil {
log.Fatal(err)
}
// Validate each entry
for i, ex := range exercises {
if err := schema.Validate(ex); err != nil {
fmt.Printf("Exercise #%d validation error: %v\n", i+1, err)
}
}
}
Summary
- The exercises dataset includes
data/exercises.schema.jsonfollowing JSON Schema Draft 2020-12. - Required fields include multilingual instruction objects with specific language keys (
en,es,it,tr,ru,zh,hi,pl,ko). - Python users can rely on the
jsonschemalibrary for validation. - Node.js developers should use AJV for comprehensive error reporting.
- Command-line validation is available through ajv-cli for CI/CD pipelines.
- Go applications can leverage
santhosh-tekuri/jsonschema/v5for type-safe validation.
Frequently Asked Questions
What JSON Schema draft does the exercises dataset use?
The schema follows Draft 2020-12 of the JSON Schema specification. This modern draft supports advanced validation features like unevaluatedProperties and strict URI formats, making it compatible with validators such as AJV and jsonschema (Python) that offer Draft 2020-12 support.
Which fields are required in the exercises schema?
The schema mandates several top-level fields including id, name, category, body_part, and created_at. Additionally, the instructions object requires exactly nine specific language keys. Missing any of these required fields or language codes will trigger a validation error.
How do I validate only a single exercise record instead of the full array?
Extract the single object from your JSON file and pass it directly to your validator's validate function. In Python, load the specific object and call validate(instance=single_exercise, schema=schema). Most validators accept individual objects or arrays, so ensure your code iterates correctly based on whether you're validating one record or many.
What happens if an exercise is missing a language key in instructions?
The validator will report a validation error indicating that a required property is missing. The schema enforces that instructions must contain all nine language keys (en, es, it, tr, ru, zh, hi, pl, ko), ensuring consistent multilingual coverage across the dataset.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →