Skip to content

Rushabh5000/json-validate

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

json-validate

Zero dependencies Node License: MIT Platform

Validate JSON data against a JSON Schema (draft-07 core) with readable error messages. Zero dependencies.

The natural companion to json-schema-gen: generate a schema from a sample, then enforce it in CI. Unlike ajv-cli, this pulls in no dependencies.


Install

npm install -g json-validate

Or without installing:

npx json-validate schema.json data.json

Usage

json-validate schema.json data.json                    # Validate one file
json-validate schema.json user1.json user2.json        # Validate several
json-validate -s schema.json data.json --quiet         # Exit code only
json-validate schema.json data.json --json             # Machine-readable

Example

schema.json:

{
  "type": "object",
  "required": ["id", "email", "age"],
  "properties": {
    "id":    { "type": "string", "format": "uuid" },
    "email": { "type": "string", "format": "email" },
    "age":   { "type": "integer", "minimum": 0, "maximum": 120 },
    "role":  { "enum": ["admin", "user", "guest"] }
  },
  "additionalProperties": false
}

data.json:

{ "id": "not-a-uuid", "email": "bad", "age": 200, "role": "wizard", "extra": true }

Running json-validate schema.json data.json:

json-validate  schema: schema.json

  ✗ data.json  5 errors
      .id     is not a valid uuid
      .email  is not a valid email
      .age    must be <= 120 (is 200)
      .role   must be one of ["admin","user","guest"]
      (root)  additional property 'extra' is not allowed

  0 valid  1 invalid

Supported Keywords

Category Keywords
Core type, enum, const
Object required, properties, patternProperties, additionalProperties, minProperties, maxProperties
Array items (incl. tuples), minItems, maxItems, uniqueItems
Number minimum, maximum, exclusiveMinimum, exclusiveMaximum, multipleOf
String minLength, maxLength, pattern, format
Combinators allOf, anyOf, oneOf, not

Formats: date-time, date, time, email, uri, uuid, ipv4, ipv6, hostname


CI Integration

- name: Validate config against schema
  run: npx json-validate config.schema.json config.json --quiet

Exit codes:

  • 0 — all data files valid
  • 1 — at least one invalid
  • 2 — file missing or malformed JSON

Scope

This validates the draft-07 core keywords listed above — covering the vast majority of real-world schemas. It does not resolve $ref/$defs or remote references; keep schemas self-contained. For full draft-2020-12 + $ref resolution, use ajv.


License

MIT


Keywords

json schema validator · validate json · ajv alternative · ajv-cli alternative · json validation · draft-07 · schema check · validate against schema · zero dependencies · ci


Built to solve, shared to help — Rushabh Shah 🛠️✨

One of 40+ zero-dependency developer CLI tools — no node_modules, ever.

Releases

No releases published

Packages

 
 
 

Contributors