{"id":16965011,"url":"https://github.com/propensive/villainy","last_synced_at":"2026-05-09T10:15:55.885Z","repository":{"id":177185528,"uuid":"657771126","full_name":"propensive/villainy","owner":"propensive","description":"Record types from JSON schemata in Scala","archived":false,"fork":false,"pushed_at":"2025-01-22T08:37:49.000Z","size":860,"stargazers_count":1,"open_issues_count":3,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-28T20:49:20.614Z","etag":null,"topics":["compile-time-checking","json","json-schema","record-types","scala","type-construction","typesafe"],"latest_commit_sha":null,"homepage":"","language":"Scala","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/propensive.png","metadata":{"files":{"readme":".github/readme.md","changelog":null,"contributing":".github/contributing.md","funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-06-23T20:14:28.000Z","updated_at":"2025-01-22T08:37:53.000Z","dependencies_parsed_at":"2023-11-16T09:26:42.081Z","dependency_job_id":"b859e8ea-2253-445e-a398-c8ca23d1e90c","html_url":"https://github.com/propensive/villainy","commit_stats":null,"previous_names":["propensive/villainy"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/propensive%2Fvillainy","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/propensive%2Fvillainy/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/propensive%2Fvillainy/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/propensive%2Fvillainy/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/propensive","download_url":"https://codeload.github.com/propensive/villainy/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247361598,"owners_count":20926642,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["compile-time-checking","json","json-schema","record-types","scala","type-construction","typesafe"],"created_at":"2024-10-13T23:44:49.728Z","updated_at":"2026-05-09T10:15:50.865Z","avatar_url":"https://github.com/propensive.png","language":"Scala","funding_links":[],"categories":[],"sub_categories":[],"readme":"[\u003cimg alt=\"GitHub Workflow\" src=\"https://img.shields.io/github/actions/workflow/status/propensive/villainy/main.yml?style=for-the-badge\" height=\"24\"\u003e](https://github.com/propensive/villainy/actions)\n[\u003cimg src=\"https://img.shields.io/discord/633198088311537684?color=8899f7\u0026label=DISCORD\u0026style=for-the-badge\" height=\"24\"\u003e](https://discord.com/invite/MBUrkTgMnA)\n\u003cimg src=\"/doc/images/github.png\" valign=\"middle\"\u003e\n\n# Villainy\n\n__Scheming typesafe Scala record types for JSON__\n\n_Villainy_ makes it possible to construct safe record types from a JSON Schema,\nwithout any explicit Scala definitions of those types. This makes it safer and\neasier to work with JSON values that conform to a schema. Record types are\ngenerated by [Polyvinyl](https://github.com/propensive/polyvinyl) and JSON\nintegration is provided by [Jacinta](https://github.com/propensive/jacinta)\n\n## Features\n\n- reads a JSON Schema at compiletime and generates corresponding Scala structural record types\n- record types are usable and fully typesafe in downstream compilations\n- JSON Schema may originate from a file, URL, classpath or any source\n- modifications to the schema can change types in subsequent compilations\n\n\n## Availability\n\n\n\n\n\n\n\n## Getting Started\n\nA JSON Schema is a JSON document which defines invariants of the structure of other documents, most notably\nthe set of names of properties that are to be expected in the top-level JSON object; the JSON type of each\none; and recursively, the same for every nested object and the elements of each array.\n\nHere is an example, modified from [json-schema.org](https://json-schema.org/learn/miscellaneous-examples.html):\n```json\n{\n  \"$id\": \"https://example.com/person.schema.json\",\n  \"$schema\": \"https://json-schema.org/draft/2020-12/schema\",\n  \"title\": \"Person\",\n  \"type\": \"object\",\n  \"properties\": {\n    \"name\" {\n      \"type\": \"object\",\n      \"properties\": {\n        \"first\": { \"type\": \"string\" },\n        \"last\": { \"type\": \"string\" }\n      }\n    },\n    \"age\": { \"type\": \"integer\" }\n  }\n}\n```\n\nIn Scala, it might be natural to implement this data structure with case classes,\n```scala\ncase class Person(name: Name, age: Int)\ncase class Name(first: String, last: String)\n```\nbut for many cases, it may be sufficient to use this structural type,\n```scala\ntype Person = {\n  def name: { def first: String; def last: String }\n  def age: Int\n}\n```\nwhich does not require a corresponding class file to exist for the compiler to use it. (In fact, an instance\nof the case class `Person` would conform to the structural type `Person` anyway.)\n\n### Creating a schema object\n\nWe need to create an object, say, `PersonSchema`, which represents our schema. This _must_ be a singleton\nobject (and not a `val`), and should extend `JsonSchema`, which requires a single constructor parameter.\n\nThis constructor parameter may be any value that can be read as `Bytes`, so the following options would\nall work, using appropriate [Turbulence](https://github.com/propensive/turbulence) `Readable` instances:\n```scala\nval schema: Text = t\"\"\"{...}\"\"\"\nobject PersonSchema extends JsonSchema(schema)\n```\nor, using [Ambience](https://github.com/propensive/ambience/) and [Galilei](https://github.com/propensive/galilei),\n```scala\nobject PersonSchema extends JsonSchema(env.pwd / p\"data\" / p\"schema.json\")\n```\nor, using [Telekinesis](https://github.com/propensive/telekinesis/),\n```scala\nobject PersonSchema extends JsonSchema(url\"https://example.com/schemata/person.jsons\")\n```\n\nThis object must be compiled before any code which uses it to create records. It should be sufficient to put it into\na separate source file, but it can also be compiled in a separate build module.\n\nThis is because the compiler needs to instantiate it at the time it compiles the code which constructs\nrecords. It will therefore also need to execute the code which provides the JSON schema data, so in the file-based\nexample above, the file `schema.json` must be in the right place relative to the PWD *at compiletime*.\n\nFurthermore, any exceptions that may be thrown during construction must be neutralized.\n\n### Constructing the record\n\nGiven a `JsonSchema` object instance corresponding to our particular schema, _Villainy_\nmakes it possible to construct a new record instance from an untyped `Json` value with a structural type\nderived from the JSON schema above, *without* needing to define the type for that schema:\n```scala\nimport jacinta.*\nimport villainy.*\n\nval json: Json = Json.parse(t\"\"\"{ \"name\": { \"first\": \"Douglas\", \"last\": \"Richards\" }, \"age\": 22 }\"\"\")\nval person = PersonSchema.record(json)\n```\n\nWe can then safely access fields such as, `person.name.first` (a `String`) and `person.age` (an `Int`), but\nattempts to access fields not defined in the schema, such as `person.lastName`, will be compile errors.\n\n\n\n\n## Status\n\nVillainy is classified as __embryonic__. For reference, Soundness projects are\ncategorized into one of the following five stability levels:\n\n- _embryonic_: for experimental or demonstrative purposes only, without any guarantees of longevity\n- _fledgling_: of proven utility, seeking contributions, but liable to significant redesigns\n- _maturescent_: major design decisions broady settled, seeking probatory adoption and refinement\n- _dependable_: production-ready, subject to controlled ongoing maintenance and enhancement; tagged as version `1.0.0` or later\n- _adamantine_: proven, reliable and production-ready, with no further breaking changes ever anticipated\n\nProjects at any stability level, even _embryonic_ projects, can still be used,\nas long as caution is taken to avoid a mismatch between the project's stability\nlevel and the required stability and maintainability of your own project.\n\nVillainy is designed to be _small_. Its entire source code currently consists\nof 356 lines of code.\n\n## Building\n\nVillainy will ultimately be built by Fury, when it is published. In the\nmeantime, two possibilities are offered, however they are acknowledged to be\nfragile, inadequately tested, and unsuitable for anything more than\nexperimentation. They are provided only for the necessity of providing _some_\nanswer to the question, \"how can I try Villainy?\".\n\n1. *Copy the sources into your own project*\n   \n   Read the `fury` file in the repository root to understand Villainy's build\n   structure, dependencies and source location; the file format should be short\n   and quite intuitive. Copy the sources into a source directory in your own\n   project, then repeat (recursively) for each of the dependencies.\n\n   The sources are compiled against the latest nightly release of Scala 3.\n   There should be no problem to compile the project together with all of its\n   dependencies in a single compilation.\n\n2. *Build with [Wrath](https://github.com/propensive/wrath/)*\n\n   Wrath is a bootstrapping script for building Villainy and other projects in\n   the absence of a fully-featured build tool. It is designed to read the `fury`\n   file in the project directory, and produce a collection of JAR files which can\n   be added to a classpath, by compiling the project and all of its dependencies,\n   including the Scala compiler itself.\n   \n   Download the latest version of\n   [`wrath`](https://github.com/propensive/wrath/releases/latest), make it\n   executable, and add it to your path, for example by copying it to\n   `/usr/local/bin/`.\n\n   Clone this repository inside an empty directory, so that the build can\n   safely make clones of repositories it depends on as _peers_ of `villainy`.\n   Run `wrath -F` in the repository root. This will download and compile the\n   latest version of Scala, as well as all of Villainy's dependencies.\n\n   If the build was successful, the compiled JAR files can be found in the\n   `.wrath/dist` directory.\n\n## Contributing\n\nContributors to Villainy are welcome and encouraged. New contributors may like\nto look for issues marked\n[beginner](https://github.com/propensive/villainy/labels/beginner).\n\nWe suggest that all contributors read the [Contributing\nGuide](/contributing.md) to make the process of contributing to Villainy\neasier.\n\nPlease __do not__ contact project maintainers privately with questions unless\nthere is a good reason to keep them private. While it can be tempting to\nrepsond to such questions, private answers cannot be shared with a wider\naudience, and it can result in duplication of effort.\n\n## Author\n\nVillainy was designed and developed by Jon Pretty, and commercial support and\ntraining on all aspects of Scala 3 is available from [Propensive\nO\u0026Uuml;](https://propensive.com/).\n\n\n\n## Name\n\nSchema is homophonous with _schemer_, that is, someone who formulates devious plans; presumably an act of _villainy_.\n\nIn general, Soundness project names are always chosen with some rationale,\nhowever it is usually frivolous. Each name is chosen for more for its\n_uniqueness_ and _intrigue_ than its concision or catchiness, and there is no\nbias towards names with positive or \"nice\" meanings—since many of the libraries\nperform some quite unpleasant tasks.\n\nNames should be English words, though many are obscure or archaic, and it\nshould be noted how willingly English adopts foreign words. Names are generally\nof Greek or Latin origin, and have often arrived in English via a romance\nlanguage.\n\n## Logo\n\nThe logo shows a pair of glowing, villainous eyes.\n\n## License\n\nVillainy is copyright \u0026copy; 2025 Jon Pretty \u0026 Propensive O\u0026Uuml;, and\nis made available under the [Apache 2.0 License](/license.md).\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpropensive%2Fvillainy","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpropensive%2Fvillainy","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpropensive%2Fvillainy/lists"}