about summary refs log tree commit diff
path: root/pkgs/by-name/cu/cue/validator.nix
diff options
context:
space:
mode:
Diffstat (limited to 'pkgs/by-name/cu/cue/validator.nix')
-rw-r--r--pkgs/by-name/cu/cue/validator.nix17
1 files changed, 17 insertions, 0 deletions
diff --git a/pkgs/by-name/cu/cue/validator.nix b/pkgs/by-name/cu/cue/validator.nix
new file mode 100644
index 0000000000000..cddc3fe342b72
--- /dev/null
+++ b/pkgs/by-name/cu/cue/validator.nix
@@ -0,0 +1,17 @@
+{ cue, writeShellScript, lib }:
+# `document` must be a fragment of definition or structure that the input data will be matched against.
+# `document` must exist in the Cue schema file provided (`cueSchemaFile`).
+# The result is a script that can be used to validate the input data (JSON/YAML and more can be supported depending on Cue)
+# against the fragment described by `document` or the whole definition.
+# The script will be strict and enforce concrete values, i.e. partial documents are not supported.
+cueSchemaFile: { document ? null }:
+  writeShellScript "validate-using-cue"
+  ''${cue}/bin/cue \
+      --all-errors \
+      --strict \
+      vet \
+      --concrete \
+      "$1" \
+      ${cueSchemaFile} \
+      ${lib.optionalString (document != null) "-d \"${document}\""}
+  ''