{"id":20464779,"url":"https://github.com/crdsonnet/validate-libsonnet","last_synced_at":"2025-09-24T22:31:16.034Z","repository":{"id":175285967,"uuid":"653613946","full_name":"crdsonnet/validate-libsonnet","owner":"crdsonnet","description":"Type checking for Jsonnet","archived":false,"fork":false,"pushed_at":"2025-01-13T09:04:09.000Z","size":214,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-01-13T09:32:14.552Z","etag":null,"topics":["json-schema","json-schema-validator","jsonnet","jsonnet-lib"],"latest_commit_sha":null,"homepage":"","language":"Jsonnet","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/crdsonnet.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","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-14T11:42:42.000Z","updated_at":"2025-01-13T09:04:12.000Z","dependencies_parsed_at":null,"dependency_job_id":"39407dc5-e383-48e2-b969-d3386f3adb9d","html_url":"https://github.com/crdsonnet/validate-libsonnet","commit_stats":null,"previous_names":["duologic/jsonnet-type-checking-poc","crdsonnet/validate-libsonnet"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/crdsonnet%2Fvalidate-libsonnet","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/crdsonnet%2Fvalidate-libsonnet/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/crdsonnet%2Fvalidate-libsonnet/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/crdsonnet%2Fvalidate-libsonnet/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/crdsonnet","download_url":"https://codeload.github.com/crdsonnet/validate-libsonnet/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":234134505,"owners_count":18784840,"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":["json-schema","json-schema-validator","jsonnet","jsonnet-lib"],"created_at":"2024-11-15T13:16:27.785Z","updated_at":"2025-09-24T22:31:10.676Z","avatar_url":"https://github.com/crdsonnet.png","language":"Jsonnet","funding_links":[],"categories":[],"sub_categories":[],"readme":"# validate-libsonnet\n\nType checking is a common grievance in the jsonnet eco-system, this library is an\naid to validate function parameters and other values.\n\nHere's a comprehensive example validating the function arguments against the\narguments documented by docsonnet:\n\n```jsonnet\nlocal validate = import 'github.com/crdsonnet/validate-libsonnet/main.libsonnet';\nlocal d = import 'github.com/jsonnet-libs/docsonnet/doc-util/main.libsonnet';\n\n{\n  '#func'::\n    d.func.new(\n      'sample function',\n      args=[\n        d.arg('num', d.T.number),\n        d.arg('str', d.T.string),\n        d.arg('enum', d.T.string, enums=['valid', 'values']),\n      ],\n    ),\n  func(num, str, enum)::\n    assert validate.checkParamsFromDocstring(\n      [num, str, enum],\n      self['#func'],\n    );\n    {/* do something here */ },\n\n  return: self.func(100, 20, 'invalid'),\n}\n\n```\n\nA failure output would look like this:\n\n```\nTRACE: main.libsonnet:95 \nInvalid parameters:\n  Parameter enum is invalid:\n    Value \"invalid\" MUST match schema:\n      {\n        \"enum\": [\n          \"valid\",\n          \"values\"\n        ],\n        \"type\": \"string\"\n      }\n  Parameter str is invalid:\n    Value 20 MUST match schema:\n      {\n        \"type\": \"string\"\n      }\nRUNTIME ERROR: Assertion failed\n\texample/fromdocstring.jsonnet:(15:5)-(19:31)\t\n\texample/fromdocstring.jsonnet:21:11-40\tobject \u003canonymous\u003e\n\tField \"return\"\t\n\tDuring manifestation\t\n\n\n```\n\n## Install\n\n```\njb install github.com/crdsonnet/validate-libsonnet@master\n```\n\n## Usage\n\n```jsonnet\nlocal validate = import 'github.com/crdsonnet/validate-libsonnet/main.libsonnet'\n```\n\n\n## Index\n\n* [`fn checkParameters(checks)`](#fn-checkparameters)\n* [`fn checkParamsFromDocstring(params, docstring)`](#fn-checkparamsfromdocstring)\n* [`fn crdCheck(object, crds)`](#fn-crdcheck)\n* [`fn getChecksFromDocstring(params, docstring)`](#fn-getchecksfromdocstring)\n* [`fn schemaCheck(param, schema)`](#fn-schemacheck)\n\n## Fields\n\n### fn checkParameters\n\n```jsonnet\ncheckParameters(checks)\n```\n\nPARAMETERS:\n\n* **checks** (`object`)\n\n`checkParameters` validates parameters against their `checks`.\n\n```jsonnet\nlocal validate = import 'github.com/crdsonnet/validate-libsonnet/main.libsonnet';\n\nlocal func(arg) =\n  assert validate.checkParameters({\n    arg: std.isString(arg),\n  });\n  {/* do something here */ };\n\nfunc(false)\n\n```\n\nA failure output would look like this:\n\n```\nTRACE: main.libsonnet:95 \nInvalid parameters:\n  Parameter enum is invalid:\n    Value \"invalid\" MUST match schema:\n      {\n        \"enum\": [\n          \"valid\",\n          \"values\"\n        ],\n        \"type\": \"string\"\n      }\n  Parameter str is invalid:\n    Value 20 MUST match schema:\n      {\n        \"type\": \"string\"\n      }\nRUNTIME ERROR: Assertion failed\n\texample/fromdocstring.jsonnet:(15:5)-(19:31)\t\n\texample/fromdocstring.jsonnet:21:11-40\tobject \u003canonymous\u003e\n\tField \"return\"\t\n\tDuring manifestation\t\n\n\n```\n\n### fn checkParamsFromDocstring\n\n```jsonnet\ncheckParamsFromDocstring(params, docstring)\n```\n\nPARAMETERS:\n\n* **params** (`array`)\n* **docstring** (`object`)\n\n`checkParamsFromDocstring` validates `params` against a docsonnet `docstring` object.\n\n```jsonnet\nlocal validate = import 'github.com/crdsonnet/validate-libsonnet/main.libsonnet';\nlocal d = import 'github.com/jsonnet-libs/docsonnet/doc-util/main.libsonnet';\n\n{\n  '#func'::\n    d.func.new(\n      'sample function',\n      args=[\n        d.arg('num', d.T.number),\n        d.arg('str', d.T.string),\n        d.arg('enum', d.T.string, enums=['valid', 'values']),\n      ],\n    ),\n  func(num, str, enum)::\n    assert validate.checkParamsFromDocstring(\n      [num, str, enum],\n      self['#func'],\n    );\n    {/* do something here */ },\n\n  return: self.func(100, 20, 'invalid'),\n}\n\n```\n\nA failure output would look like this:\n\n```\nTRACE: main.libsonnet:95 \nInvalid parameters:\n  Parameter enum is invalid:\n    Value \"invalid\" MUST match schema:\n      {\n        \"enum\": [\n          \"valid\",\n          \"values\"\n        ],\n        \"type\": \"string\"\n      }\n  Parameter str is invalid:\n    Value 20 MUST match schema:\n      {\n        \"type\": \"string\"\n      }\nRUNTIME ERROR: Assertion failed\n\texample/fromdocstring.jsonnet:(15:5)-(19:31)\t\n\texample/fromdocstring.jsonnet:21:11-40\tobject \u003canonymous\u003e\n\tField \"return\"\t\n\tDuring manifestation\t\n\n\n```\n\n### fn crdCheck\n\n```jsonnet\ncrdCheck(object, crds)\n```\n\nPARAMETERS:\n\n* **object** (`object`)\n* **crds** (`array`)\n\n`crdCheck` validates `object` against a set of CRDs.\n\n```jsonnet\nlocal validate = import 'github.com/crdsonnet/validate-libsonnet/main.libsonnet';\n\nlocal crds = std.parseYaml(importstr './crds.yaml');\nlocal object = {\n  kind: 'ContactPoint',\n  apiVersion: 'alerting.grafana.crossplane.io/v1alpha1',\n  spec: {\n    deletionPolicy: null,\n    forProvider: {},\n  },\n};\n\nassert validate.crdCheck(object, crds); true\n\n\n```\n\nA failure output would look like this:\n\n```\nTRACE: validate.libsonnet:24 ERR invalid enum, value should be one of [\n    \"Orphan\",\n    \"Delete\"\n]: \nnull\nRUNTIME ERROR: Assertion failed\n\texample/crdCheck.jsonnet:13:1-45\t\n\tDuring evaluation\t\n\n\n```\n\n### fn getChecksFromDocstring\n\n```jsonnet\ngetChecksFromDocstring(params, docstring)\n```\n\nPARAMETERS:\n\n* **params** (`array`)\n* **docstring** (`object`)\n\n`getChecksFromDocstring` returns checks for `params` derived from a docsonnet `docstring` object.\n### fn schemaCheck\n\n```jsonnet\nschemaCheck(param, schema)\n```\n\nPARAMETERS:\n\n* **param** (`any`)\n* **schema** (`object`)\n\n`schemaCheck` validates `param` against a JSON `schema`. Note that this function does not resolve \"$ref\" and recursion.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcrdsonnet%2Fvalidate-libsonnet","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcrdsonnet%2Fvalidate-libsonnet","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcrdsonnet%2Fvalidate-libsonnet/lists"}