npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

file-json-validator

v1.0.0

Published

Check if files in selected directories has the same structure.

Downloads

330

Readme

File Json Validator

This npm package is designed to simplify the comparison of directory structures and JSON files within them. Whether you're managing translation files with directory per language or any other scenario where maintaining consistent file structures is crucial. This tool will help you in your project.

Example use case

If you have translation files in json where each language has its own directory you can use it to check if all languages has the same file structure and all keys in json files.

Example:

"scripts": {
  "translations-check": "fjv compare ./public/locales en",
  "translations-check:warn": "fjv compare ./public/locales en --only-warn",
  "translations-check-specific": "fjv dir ./public/locales/en ./public/locales/pl"
}

And run as

pnpm run translations-check

Can be used also in the CI - if there are any errors it will exit with error.

Features

  • Directory structure comparison - quickly compare if pointed directories has the same files.
  • JSON file key comparison - compare JSON's to check if their structure is the same (this compares only keys, not the values!).

Demo

demo.gif

Instalation

To install simply use your package manager to install file-json-validator.

pnpm add file-json-validator

Usage

CLI

Commands

| Command | Description | | ------------- | --------------------------------------------------------- | | fjv compare | Compare content of directories inside selected directory. | | fjv dir | Compare selected directories. | | fjv json | Compare selected json files. |

Flags:

| Flag | Description | Command | | -------------------- | ---------------------------------------------- | ------------------------ | | --only-warn | Do not exit with error if there are any diffs. | compare, dir, json | | --show-only-errors | Show only errors. | compare, dir, json | | --only-structure | Check only file structure. | compare, dir | | --only-json | Check only json structure. | compare, dir |

Commands description

Compare

Compare content of directories inside selected directory. It will check if all directories have the same json files and if the json files has the same structures. First argument is path to the directory and the second one is the main directory that others will be compared to.

If your files are structured like this:

./public
└── locales
    ├── en
    │   ├── buttons.json
    │   └── common.json
    ├── ger
    │   ├── buttons.json
    │   └── common.json
    └── pl
        └── common.json

Then using this:

fjv compare ./public/locales en

Will compare the directories ger and pl and its files with the main one en.

If any of the directories has different files or the files has different structure, it will show the differences and will exit with an error.

If you don't want it to exit with error you can use --only-warn flag.

If you don't want to see confirmation ==> OK and only see errors/warnings you can use --show-only-errors flag.

If you want to check only the file structure, you can use --only-structure flag.

If you want to check only the json structure, you can use --only-json flag.

Example output:

Directory structure: (1 errors)
./public/locales/pl
         -buttons.json
./public/locales/ger  ==> OK
Json content: (3 errors)
./public/locales/ger/buttons.json  ==> OK
./public/locales/pl/common.json  ==> OK
./public/locales/ger/common.json
         -calc.minus
         -calc.equal
         +no

Dir

If you want to compare selected directories you can use dir command. It will work the same as the compare command, but you can select directories to compare.

fjv dir ./public/locales/en ./public/locales/ger ./public/locales/pl

Example output:

Directory structure: (1 errors)
./public/locales/pl
         -buttons.json
./public/locales/ger  ==> OK
Json content: (3 errors)
./public/locales/ger/buttons.json  ==> OK
./public/locales/pl/common.json  ==> OK
./public/locales/ger/common.json
         -calc.minus
         -calc.equal
         +no

Json

If you want to compare selected json files you can use json command. It will work the same as the compare command, but you can select json files to compare.

fjv json ./public/en/buttons.json ./public/ger/buttons.json ./public/pl/buttons.json

Example output:

Json: (3 errors)
./public/pl/common.json  ==> OK
./public/ger/common.json
         -calc.minus
         -calc.equal
         +no

API

There are exported functions that you can import in your project:

import {
  compareJsonsInDirs,
  compareDirectoriesContent,
  compareJsonsFiles,
  compareJsonObjects,
} from "file-json-validator";
  • compareJsonsInDirs - compare json files in directories.
  • compareDirectoriesContent - check if directories has the same files.
  • compareJsonsFiles - compare json files.
  • compareJsonObjects - compare json objects.

There are provided types for those functions.