1"use strict";(self.webpackChunkkcl_website=self.webpackChunkkcl_website||[]).push([[678],{3905:(e,t,a)=>{a.d(t,{Zo:()=>d,kt:()=>f});var n=a(67294);function r(e,t,a){return t in e?Object.defineProperty(e,t,{value:a,enumerable:!0,configurable:!0,writable:!0}):e[t]=a,e}function o(e,t){var a=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);t&&(n=n.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),a.push.apply(a,n)}return a}function l(e){for(var t=1;t<arguments.length;t++){var a=null!=arguments[t]?arguments[t]:{};t%2?o(Object(a),!0).forEach((function(t){r(e,t,a[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(a)):o(Object(a)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(a,t))}))}return e}function i(e,t){if(null==e)return{};var a,n,r=function(e,t){if(null==e)return{};var a,n,r={},o=Object.keys(e);for(n=0;n<o.length;n++)a=o[n],t.indexOf(a)>=0||(r[a]=e[a]);return r}(e,t);if(Object.getOwnPropertySymbols){var o=Object.getOwnPropertySymbols(e);for(n=0;n<o.length;n++)a=o[n],t.indexOf(a)>=0||Object.prototype.propertyIsEnumerable.call(e,a)&&(r[a]=e[a])}return r}var c=n.createContext({}),s=function(e){var t=n.useContext(c),a=t;return e&&(a="function"==typeof e?e(t):l(l({},t),e)),a},d=function(e){var t=s(e.components);return n.createElement(c.Provider,{value:t},e.children)},p="mdxType",u={inlineCode:"code",wrapper:function(e){var t=e.children;return n.createElement(n.Fragment,{},t)}},m=n.forwardRef((function(e,t){var a=e.components,r=e.mdxType,o=e.originalType,c=e.parentName,d=i(e,["components","mdxType","originalType","parentName"]),p=s(a),m=r,f=p["".concat(c,".").concat(m)]||p[m]||u[m]||o;return a?n.createElement(f,l(l({ref:t},d),{},{components:a})):n.createElement(f,l({ref:t},d))}));function f(e,t){var a=arguments,r=t&&t.mdxType;if("string"==typeof e||r){var o=a.length,l=new Array(o);l[0]=m;var i={};for(var c in t)hasOwnProperty.call(t,c)&&(i[c]=t[c]);i.originalType=e,i[p]="string"==typeof e?e:r,l[1]=i;for(var s=2;s<o;s++)l[s]=a[s];return n.createElement.apply(null,l)}return n.createElement.apply(null,a)}m.displayName="MDXCreateElement"},6544:(e,t,a)=>{a.r(t),a.d(t,{assets:()=>c,contentTitle:()=>l,default:()=>u,frontMatter:()=>o,metadata:()=>i,toc:()=>s});var n=a(87462),r=(a(67294),a(3905));const o={sidebar_position:7},l="Validation",i={unversionedId:"tools/cli/kcl/vet",id:"tools/cli/kcl/vet",title:"Validation",description:"Introduction",source:"@site/docs/tools/cli/kcl/vet.md",sourceDirName:"tools/cli/kcl",slug:"/tools/cli/kcl/vet",permalink:"/docs/next/tools/cli/kcl/vet",draft:!1,editUrl:"https://github.com/kcl-lang/kcl-lang.io/tree/main/docs/tools/cli/kcl/vet.md",tags:[],version:"current",lastUpdatedBy:"Peefy",lastUpdatedAt:1790423302,formattedLastUpdatedAt:"Sep 26, 2026",sidebarPosition:7,frontMatter:{sidebar_position:7},sidebar:"tools",previous:{title:"Test",permalink:"/docs/next/tools/cli/kcl/test"},next:{title:"kcl mod init",permalink:"/docs/next/tools/cli/package-management/command-reference/init"},gitCommit:{short:"2d56980",url:"https://github.com/kcl-lang/kcl-lang.io/commit/2d569802e4985ab77474df12d6b6297eca8d2a21"}},c={},s=[{value:"Introduction",id:"introduction",level:2},{value:"How to use",id:"how-to-use",level:2},{value:"Specify the schema for validation",id:"specify-the-schema-for-validation",level:2},{value:"Args",id:"args",level:2}],d={toc:s},p="wrapper";
1function u(e){let{components:t,...a}=e;return(0,r.kt)(p,(0,n.Z)({},d,a,{components:t,mdxType:"MDXLayout"}),(0,r.kt)("h1",{id:"validation"},"Validation"),(0,r.kt)("h2",{id:"introduction"},"Introduction"),(0,r.kt)("p",null,"The KCL Validation tool supports basic configuration data verification capabilities. You can write a KCL schema to verify the type and value of the input JSON/YAML files."),(0,r.kt)("h2",{id:"how-to-use"},"How to use"),(0,r.kt)("p",null,"There is a JSON format file ",(0,r.kt)("inlineCode",{parentName:"p"},"data.json"),":"),(0,r.kt)("pre",null,(0,r.kt)("code",{parentName:"pre",className:"language-json"},'{\n "name": "Alice",\n "age": 18,\n "message": "This is Alice",\n "data": {\n "id": 1,\n "value": "value1"\n },\n "labels": {\n "key": "value"\n },\n "hc": [1, 2, 3]\n}\n')),(0,r.kt)("p",null,"Build a validated KCL file ",(0,r.kt)("inlineCode",{parentName:"p"},"schema.k"),":"),(0,r.kt)("pre",null,(0,r.kt)("code",{parentName:"pre",className:"language-py"},"schema User:\n name: str\n age: int\n message?: str\n data: Data\n labels: {str:}\n hc: [int]\n\n check:\n age > 10\n\nschema Data:\n id: int\n value: str\n")),(0,r.kt)("p",null,"Execute the following command:"),(0,r.kt)("pre",null,(0,r.kt)("code",{parentName:"pre",className:"language-shell"},"kcl vet data.json schema.k\n")),(0,r.kt)("h2",{id:"specify-the-schema-for-validation"},"Specify the schema for validation"),(0,r.kt)("p",null,"When multiple schema definitions exist in the KCL file, by default, the KCL Validation tool will use the first schema to check. If you need to specify a schema for verification, you can use the ",(0,r.kt)("inlineCode",{parentName:"p"},"-s|--schema")," parameter"),(0,r.kt)("pre",null,(0,r.kt)("code",{parentName:"pre",className:"language-shell"},"kcl vet data.json schema.k -s User\n")),(0,r.kt)("h2",{id:"args"},"Args"),(0,r.kt)("pre",null,(0,r.kt)("code",{parentName:"pre",className:"language-shell"},'This command validates the data file using the kcl code.\n\nUsage:\n kcl vet [flags]\n\nExamples:\n # Validate the JSON data using the kcl code\n kcl vet data.json code.k\n\n # Validate the YAML data using the kcl code\n kcl vet data.yaml code.k --format yaml\n\n # Validate the JSON data using the kcl code with the schema name\n kcl vet data.json code.k -s Schema\n\n # Validate and output results as JSON for CI/CD integration\n kcl vet data.json code.k --output json\n\n # Validate against a schema that imports an external KCL package\n kcl vet data.yaml schema.k -E my_pkg=./vendor/my_pkg\n\nFlags:\n -a, --attribute_name string Specify the validate config attribute name.\n -E, --external stringArray Specify the mapping of package name and path where the package is located, e.g. my_pkg=./vendor/my_pkg\n --format string Specify the validate data format. e.g., yaml, json. Default is json\n -h, --help help for vet\n --output string Specify the output format. e.g., text, json. Default is text (default "text")\n -s, --schema string Specify the validate schema.\n')))}u.isMDXComponent=!0}}]);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.