Keep YAML and JSON files organised by checking their property order against a JSON schema.
Use order if:
- You have many contributors to a repository and want to enforce style consistency
- You want to reduce mental effort in code reviews with a predictable pattern.
- You want your YAML or JSON files to stay neat and consistent.
go get github.com/roscrl/orderpackage main
import (
"fmt"
"github.com/roscrl/order"
)
func main() {
// Check if YAML file conforms to property order defined in JSON schema
err := order.Lint("config.yaml", "schema.json")
if err != nil {
return fmt.Errorf("linting properties order against json schema: %v", err)
}
}order looks at the properties list in your JSON schema and makes sure your YAML or JSON file follows the same order.
Properties need to match the schema's order (e.g., "name" before "version").
{
"properties": {
"name": { "type": "string" },
"version": { "type": "string" },
"description": { "type": "string" },
"dependencies": {
"properties": {
"production": { "type": "object" },
"development": { "type": "object" }
}
},
"scripts": { "type": "object" }
}
}name: my-package
version: 1.0.0
description: A sample package
dependencies:
production:
some-lib: ^1.0.0
development:
test-lib: ^2.0.0
scripts:
test: jestversion: 1.0.0 # Should come after 'name'
name: my-package
description: A sample package
dependencies:
development: # Should come after 'production'
test-lib: ^2.0.0
production:
some-lib: ^1.0.0
scripts:
test: jestThis causes an error: "version" is out of order; it should come after "name".
See examples/main.go for a complete example.