An open API service indexing awesome lists of open source software.

https://github.com/lesomnus/cray

Schema description framework
https://github.com/lesomnus/cray

json json-schema schema-generator validator yaml

Last synced: 2 months ago
JSON representation

Schema description framework

Awesome Lists containing this project

README

        

# CRay

[![test](https://github.com/lesomnus/cray/actions/workflows/test.yaml/badge.svg)](https://github.com/lesomnus/cray/actions/workflows/test.yaml)
[![codecov](https://codecov.io/gh/lesomnus/cray/branch/main/graph/badge.svg?token=Zw14Luij1P)](https://codecov.io/gh/lesomnus/cray)

Access configs in a structured manner with validation and automatically generate documented configs.

CRay creates a schema for your data by tracking how you access it. You don't have to keep checking each value in the middle of your code to see if it satisfies the condition you want. Validate documents at once with an automatically generated schema, and see at a glance what values went wrong.

## Support

### Loaders

- YAML (powered by [jbeder/yaml-cpp](https://github.com/jbeder/yaml-cpp))

### Reporters

- YAML
- JSON Schema

## CMake Integration
```cmake
include(FetchContent)
FetchContent_Declare(
CRay
GIT_REPOSITORY https://github.com/lesomnus/cray.git
GIT_TAG main
)
FetchContent_MakeAvailable(CRay)

...

add_library(foo ...)
...
target_link_libraries(
foo PRIVATE
CRay::CRay
CRay::yaml # YAML loader support
)
```

Note that `CRay::yaml` is enabled only if the `yaml-cpp` package is available.

## Example

You may want to read what kind of [describers](docs/describers.md) are available.

```cpp
#include
#include
#include
#include
#include

#include

struct Step {
std::string name;
std::string run;
};

struct Job {
std::vector runs_on;
std::vector steps;
};

int main(int argc, char*[] argv) {
using namespace cray;

Node doc(load::fromYaml("workflow.yaml"));

auto const name = doc["name"].as(Annotation{
.title = "Name",
.description = "The name of your workflow.",
});

auto const run_name = doc["run-name"].as>(Annotation{
.title = "Run Name",
.description = "The name for workflow runs generated from the workflow.",
});

auto events = prop().oneOf({
"push",
"pull_request",
"workflow_dispatch",
});

auto const on = doc["on"].is().of(events).get();

auto step =
prop().to()
| field("name", &Step::name)
| field("run", &Step::run);

auto job =
prop().to()
| field("runs-on", &Job::runs_on)
| field("steps", &Job::steps, step);

auto const jobs = doc["jobs"].is().of(job).get();

std::ofstream out("workflow.yaml");
report::asYaml(out, doc);

std::ofstream out("workflow.schema.json");
report::asJsonSchema(out, doc);

if(!doc.ok()){
// There is a field that does not satisfy the condition or has the wrong type.
return -1;
}

// name == "Test"
// jobs["test"].steps[0].run == "build && test"
// ...
}
```

Consider original `workflow.yaml`:
```yaml
name: Test
on: [push, pull_request]
jobs:
test:
runs-on: [ubuntu-20.04]
steps:
- name: Test
run: build && test
```

Will be:
```yaml
# Name
# | The name of your workflow.
name: Test

# Run Name
# | The name for workflow runs generated from the workflow.
run-name: #

on:
# • push | pull_request | workflow_dispatch
[push, pull_request]
jobs:
test:
runs-on: [ubuntu-20.04]
steps:
-
name: Test
run: build && test
```

And you can have JSON schema `workflow.schema.json`:

> This is a summarized result. Full results can be found at [tests/src/example-report.cpp](tests/src/example-report.cpp).

```json
{
"type": "object",
"required": [
"name",
"on",
"jobs"
],
"properties": {
"name": {
"type": "string",
"title": "Name",
"description": "The name of your workflow."
},
"run-name": {
"type": "string",
"title": "Run Name",
"description": "The name for workflow runs generated from the workflow."
},
"on": {
"type": "array",
"items": {
"type": "string",
"enum": [
"push",
"pull_request",
"workflow_dispatch"
]
}
},
"jobs": {
...
}
}
}
```