@deconz-community/ddf-validator 2.22.0 → 2.24.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/ddf-validator.cjs +1 -1
- package/dist/ddf-validator.d.ts +2 -0
- package/dist/ddf-validator.mjs +1 -1
- package/package.json +14 -15
package/dist/ddf-validator.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("zod"),G="2.22.0",w={"devcap1.schema.json":[M,P,W,L,N,Y],"constants2.schema.json":[R]};function M(t,r,u){const i=typeof t.manufacturername=="string"&&typeof t.modelid=="string";if(i)return;const c=Array.isArray(t.manufacturername)&&Array.isArray(t.modelid);if(c&&t.manufacturername.length!==t.modelid.length){r.addIssue({code:e.z.ZodIssueCode.invalid_intersection_types,message:"When 'manufacturername' and 'modelid' are both arrays they should be the same length",path:["manufacturername","modelid"]});return}(i||c)===!1&&r.addIssue({code:e.z.ZodIssueCode.invalid_intersection_types,message:"Invalid properties 'manufacturername' and 'modelid' should have the same type",path:["manufacturername","modelid"]})}function P(t,r,u){if(!t.bindings)return;const i=n=>`0x${(typeof n=="number"?n:Number.parseInt(n,16)).toString(16)}`,c={};t.bindings.forEach(n=>{n.bind==="unicast"&&n.report&&n.report.forEach(d=>{d.max!==65535&&(c[`${i(n["src.ep"])}.${i(n.cl)}.${i(d.at)}`]=d.max)})}),t.subdevices.forEach((n,d)=>{n.items.forEach((a,m)=>{var s;if(a["refresh.interval"]&&a.read&&a.read.fn==="zcl:attr"&&a.read.at){const o=i(a.read.ep??((s=n.fingerprint)==null?void 0:s.endpoint)??n.uuid[1]),p=Array.isArray(a.read.at)?a.read.at:[a.read.at];for(let h=0;h<p.length;h++){const z=`${o}.${i(a.read.cl)}.${i(p[h])}`;c[z]!==void 0&&a["refresh.interval"]-60<c[z]&&r.addIssue({code:e.z.ZodIssueCode.custom,message:`The refresh interval (${a["refresh.interval"]} - 60 = ${a["refresh.interval"]-60}) should be greater than the binding max refresh value (${c[z]}) with a margin of 60 seconds`,path:["subdevices",d,"items",m,"refresh.interval"]})}}})})}function W(t,r,u){const i=[{description:'a device with "state/ct" need the "min" and "max" values for capability.',if:{item:"state/ct"},need:{item:["cap/color/ct/min","cap/color/ct/max"]}},{description:'a device with "cap/color/ct/computes_xy" need the corresponding state/{x,y} items',if:{item:"cap/color/ct/computes_xy"},need:{item:["state/x","state/y","state/ct"]}},{description:'a device with "state/x" or "state/y" need the corresponding cap/color/xy/{red,green,blue}/{x,y} items',if:{item:{or:["state/x","state/y"]}},need:{item:["state/x","state/y","cap/color/xy/red_x","cap/color/xy/green_x","cap/color/xy/blue_x","cap/color/xy/red_y","cap/color/xy/green_y","cap/color/xy/blue_y"]}},{description:'a "(Extended) Color light" need ("state/x" and "state/y") or/and ("state/hue" and "state/sat") .',if:{type:{or:["$TYPE_EXTENDED_COLOR_LIGHT","Extended color light","$TYPE_COLOR_LIGHT","Color light"]}},need:{item:{or:[["state/x","state/y"],["state/hue","state/sat"]]}}}],c=(n,d)=>typeof n=="string"?d(n):Array.isArray(n)?n.every(a=>c(a,d)):"and"in n?n.and.every(a=>c(a,d)):"or"in n?n.or.some(a=>c(a,d)):!1;t.subdevices.forEach((n,d)=>{const a=n.items.map(m=>m.name);i.forEach(m=>{(Object.keys(m.if).length===0||Object.entries(m.if).some(([s,o])=>{switch(s){case"type":return c(o,p=>p===n.type);case"item":return c(o,p=>a.includes(p));default:return!1}}))&&(c(m.need.item,s=>a.includes(s))||r.addIssue({code:e.z.ZodIssueCode.custom,message:`The device is missing some items because ${m.description}`,path:["subdevices",d,"items"]}))})})}function L(t,r,u){const i=["parse","write"];t.subdevices.forEach((c,n)=>{c.items.forEach((d,a)=>{i.forEach(m=>{const s=d[m];s!==void 0&&(s.fn===void 0||s.fn==="zcl:attr"||s.fn==="zcl:cmd")&&(s.eval===void 0&&s.script===void 0&&r.addIssue({code:e.z.ZodIssueCode.custom,message:`The '${m}' function is missing 'eval' or 'script' option.`,path:["subdevices",n,"items",a,m]}),s.eval!==void 0&&s.script!==void 0&&r.addIssue({code:e.z.ZodIssueCode.custom,message:`The '${m}' function is having both 'eval' and 'script' option.`,path:["subdevices",n,"items",a,m]}))})})})}function R(t,r,u){const i=D();Object.keys(t).forEach(c=>{if(!Object.keys(i.shape).includes(c)){if(!["$MF_","$TYPE_"].some(n=>c.startsWith(n))){r.addIssue({code:e.z.ZodIssueCode.custom,message:"The constant key should start with '$MF_' or '$TYPE_'",path:[c]});return}typeof t[c]!="string"&&r.addIssue({code:e.z.ZodIssueCode.custom,message:"The constant value should be a string",path:[c]})}})}function N(t,r,u){t.subdevices.forEach((i,c)=>{const n=u.subDevices[i.type];if(!n){r.addIssue({code:e.z.ZodIssueCode.custom,message:`The device is missing the device definition (subdevice1.schema.json) for the type "${i.type}"`,path:["subdevices",c,"items"]});return}let d=n.items;n.items_optional&&(d=d.filter(a=>!n.items_optional.includes(a))),d.forEach(a=>{i.items.find(m=>m.name===a)||r.addIssue({code:e.z.ZodIssueCode.custom,message:`The device should have the item "${a}" because it is mandatory for devices of type "${i.type}"`,path:["subdevices",c,"items"]})})})}function Y(t,r,u){t.subdevices.forEach((i,c)=>{const n=i.items.map(d=>d.name);i.items.forEach((d,a)=>{d.parse&&d.parse.fn==="numtostr"&&(n.includes(d.parse.srcitem)||r.addIssue({code:e.z.ZodIssueCode.custom,message:`The device should have the item "${d.parse.srcitem}" because it is used in the 'numtostr' function`,path:["subdevices",c,"items",a,"parse","srcitem"]}))})})}function B(t){return e.z.strictObject({$schema:e.z.optional(e.z.string()),schema:e.z.literal("constants1.schema.json"),ddfvalidate:e.z.optional(e.z.boolean()),manufacturers:e.z.record(e.z.string().startsWith("$MF_"),e.z.string()),"device-types":e.z.record(e.z.string().startsWith("$TYPE_"),e.z.string())})}function D(t){return e.z.object({$schema:e.z.optional(e.z.string()),schema:e.z.literal("constants2.schema.json"),ddfvalidate:e.z.optional(e.z.boolean())}).passthrough()}function U(){return e.z.string().regex(/^(\d{4})-(?:(?:0[1-9])|(?:1[0-2]))-(?:(?:0[1-9])|(?:[12][0-9])|(?:3[01]))$/,"Invalid date value")}function l(t=void 0){const r="Invalid hexadecimal value";return t===void 0?e.z.string().regex(/^0x[0-9a-fA-F]+$/,r):e.z.string().regex(new RegExp(`^0x[0-9a-fA-F]{${t}}$`),r)}function v(){return e.z.union([l(2),e.z.number().min(0).max(255)])}function V(){return e.z.custom(t=>{if(!Array.isArray(t)||t.length%2!==0)return!1;for(let r=0;r<t.length;r+=2){const u=t[r],i=t[r+1];if(typeof u!="number"||typeof i!="string")return!1}return!0},"The value must be an array with an even number of values and alternating between number and string.")}function S(){return e.z.union([e.z.tuple([e.z.literal("$address.ext"),l(2)]),e.z.tuple([e.z.literal("$address.ext"),l(2),l(4)])])}function C(){return e.z.string()}function A(){return e.z.string()}const y=e.z.strictObject({ep:e.z.optional(v()).describe("Endpoint, 255 means any endpoint, 0 means auto selected from subdevice."),cl:l(4).describe("Cluster ID."),at:e.z.optional(l(4).or(e.z.array(l(4)))).describe("Attribute ID."),mf:e.z.optional(l(4)).describe("Manufacturer code, must be set to 0x0000 for non manufacturer specific commands."),cmd:e.z.optional(l(2)).describe("Zigbee command."),eval:e.z.optional(A()).describe("Javascript expression to transform the attribute value to the Item value."),script:e.z.optional(C()).describe("Relative path of a Javascript .js file.")}),f={read:y.extend({fc:e.z.optional(l(2).or(e.z.number())).describe("Zigbee command frame control.")}),parse:y.extend({}),write:y.extend({"state.timeout":e.z.optional(e.z.number()),"change.timeout":e.z.optional(e.z.number()),dt:e.z.optional(l(2)).describe("Data type.")})};function k(){return e.z.discriminatedUnion("fn",[e.z.strictObject({fn:e.z.literal("none")}),f.read.extend({fn:e.z.undefined().describe("Generic function to read ZCL attributes.")}),f.read.extend({fn:e.z.literal("zcl:attr").describe("Generic function to parse ZCL values from read/report commands.")}),f.read.extend({fn:e.z.literal("zcl:cmd").describe("Generic function to parse ZCL values from read/report commands.")}),e.z.strictObject({fn:e.z.literal("tuya").describe("Generic function to read all Tuya datapoints. It has no parameters.")})]).refine(t=>!("eval"in t&&"script"in t),{message:"eval and script should not both be present"})}function q(t){return e.z.discriminatedUnion("fn",[f.parse.extend({fn:e.z.undefined().describe("Generic function to read ZCL attributes."),cppsrc:e.z.optional(e.z.string())}),f.parse.extend({fn:e.z.literal("zcl:attr").describe("Generic function to parse ZCL values from read/report commands.")}),f.parse.extend({fn:e.z.literal("zcl:cmd").describe("Generic function to parse ZCL values from read/report commands.")}),e.z.strictObject({fn:e.z.literal("ias:zonestatus").describe("Generic function to parse IAS ZONE status change notifications or zone status from read/report command."),mask:e.z.optional(e.z.enum(["alarm1","alarm2"]).or(e.z.literal("alarm1,alarm2"))).describe("Sets the bitmask for Alert1 and Alert2 item of the IAS Zone status.")}),e.z.strictObject({fn:e.z.literal("numtostr").describe("Generic function to to convert number to string."),srcitem:e.z.enum(t.attributes,{errorMap:(r,u)=>r.code==="invalid_enum_value"?{message:`Invalid enum value. Expected item from generic attributes definition, received '${r.received}'`}:{message:u.defaultError}}).describe("The source item holding the number."),op:e.z.enum(["lt","le","eq","gt","ge"]).describe("Comparison operator (lt | le | eq | gt | ge)"),to:V().describe("Array of (num, string) mappings")}),e.z.strictObject({fn:e.z.literal("time").describe("Specialized function to parse time, local and last set time from read/report commands of the time cluster and auto-sync time if needed.")}),f.parse.pick({ep:!0,at:!0,mf:!0,eval:!0,script:!0}).extend({fn:e.z.literal("xiaomi:special").describe("Generic function to parse custom Xiaomi attributes and commands."),idx:l(2).describe("A 8-bit string hex value.")}),f.parse.pick({eval:!0,script:!0}).extend({fn:e.z.literal("tuya").describe("Generic function to parse Tuya data."),dpid:e.z.number().describe("Data point ID. 1-255 the datapoint ID.")})]).refine(r=>!("eval"in r&&"script"in r),{message:"eval and script should not both be present"})}function H(){return e.z.discriminatedUnion("fn",[e.z.strictObject({fn:e.z.literal("none")}),f.write.extend({fn:e.z.undefined().describe("Generic function to read ZCL attributes."),cppsrc:e.z.optional(e.z.string())}),f.write.extend({fn:e.z.literal("zcl:attr").describe("Generic function to parse ZCL values from read/report commands.")}),f.write.extend({fn:e.z.literal("zcl:cmd").describe("Generic function to parse ZCL values from read/report commands.")}),f.write.pick({eval:!0,script:!0}).extend({fn:e.z.literal("tuya").describe("Generic function to write Tuya data."),dpid:e.z.number().describe("Data point ID. 1-255 the datapoint ID."),dt:l(2).describe("Data type.")})]).refine(t=>!("eval"in t&&"script"in t),{message:"eval and script should not both be present"})}function F(t){return e.z.strictObject({$schema:e.z.optional(e.z.string()),schema:e.z.literal("resourceitem1.schema.json"),id:e.z.string(),ddfvalidate:e.z.optional(e.z.boolean()),description:e.z.optional(e.z.string()).describe("Item description, better to do not use it."),comment:e.z.optional(e.z.string()).describe("TODO: What is this ? What the difference with description ?"),deprecated:e.z.optional(U()).describe("Date of deprecation, if the item is deprecated, it's better to use the new one."),datatype:e.z.optional(e.z.enum(["String","Bool","Int8","Int16","Int32","Int64","UInt8","UInt16","UInt32","UInt64","Double","Array","Array[3]","ISO 8601 timestamp"])).describe("Data type of the item."),access:e.z.optional(e.z.enum(["R","W","RW"])).describe("Access mode for this item, some of them are not editable."),public:e.z.optional(e.z.boolean()).describe("Item visible on the API."),implicit:e.z.optional(e.z.boolean()).describe("TODO: What is this ?"),managed:e.z.optional(e.z.boolean()).describe("TODO: What is this ?"),awake:e.z.optional(e.z.boolean()).describe("The device is considered awake when this item is set due a incoming command."),static:e.z.optional(e.z.union([e.z.string(),e.z.number(),e.z.boolean()])).describe("A static default value is fixed and can be not changed."),range:e.z.optional(e.z.tuple([e.z.number(),e.z.number()])).describe("Values range limit."),virtual:e.z.optional(e.z.boolean()).describe("TODO: What is this ?"),read:e.z.optional(k()).describe("Fonction used to read value."),parse:e.z.optional(q(t)).describe("Fonction used to parse incoming values."),write:e.z.optional(H()).describe("Fonction used to write value."),"refresh.interval":e.z.optional(e.z.number()).describe("Refresh interval used for read fonction, NEED to be superior at value used in binding part."),values:e.z.optional(e.z.unknown()).describe("TODO: What is this ?"),default:e.z.optional(e.z.unknown()).describe("Defaut value.")})}function J(t){return e.z.strictObject({$schema:e.z.optional(e.z.string()),schema:e.z.literal("devcap1.schema.json"),uuid:e.z.optional(e.z.string()),ddfvalidate:e.z.optional(e.z.boolean()),version:e.z.optional(e.z.string()),version_deconz:e.z.optional(e.z.string()),"doc:path":e.z.optional(e.z.string()),"doc:hdr":e.z.optional(e.z.string()),"md:known_issues":e.z.optional(e.z.array(e.z.string())).describe("Know issues for this device, markdown file."),manufacturername:e.z.union([e.z.enum(Object.keys(t.manufacturers)),e.z.string().regex(/^(?!\$MF_).*/g,"The manufacturer name start with $MF_ but is not present in constants.json"),e.z.array(e.z.union([e.z.enum(Object.keys(t.manufacturers)),e.z.string().regex(/^(?!\$MF_).*/g,"The manufacturer name start with $MF_ but is not present in constants.json")]))]).describe("Manufacturer name from Basic Cluster."),modelid:e.z.string().or(e.z.array(e.z.string())).describe("Model ID from Basic Cluster."),vendor:e.z.optional(e.z.string()).describe("Friendly name of the manufacturer."),comment:e.z.optional(e.z.string()),matchexpr:e.z.optional(A()).describe("Need to return true for the DDF be used."),path:e.z.optional(C()).describe("DDF path, useless, can be removed."),product:e.z.optional(e.z.string()).describe("Complements the model id to be shown in the UI."),sleeper:e.z.optional(e.z.boolean()).describe("Sleeping devices can only receive when awake."),supportsMgmtBind:e.z.optional(e.z.boolean()),status:e.z.enum(["Draft","Bronze","Silver","Gold"]).describe("The code quality of the DDF file."),subdevices:e.z.array(X(t)).describe("Devices section."),bindings:e.z.optional(e.z.array(Q())).describe("Bindings section.")})}function X(t){return e.z.strictObject({type:e.z.union([e.z.enum(Object.keys(t.deviceTypes),{errorMap:(r,u)=>r.code==="invalid_enum_value"?{message:`Invalid enum value. Expected type from generic attributes definition, received '${r.received}'`}:{message:u.defaultError}}),e.z.string().regex(/^(?!\$TYPE_).*/g,"The type start with $TYPE_ but is not present in constants.json")]),restapi:e.z.enum(["/lights","/sensors"]),uuid:S(),fingerprint:e.z.optional(e.z.strictObject({profile:l(4),device:l(4),endpoint:v(),in:e.z.optional(e.z.array(l(4))),out:e.z.optional(e.z.array(l(4)))})),meta:e.z.optional(e.z.strictObject({values:e.z.any(),"group.endpoints":e.z.optional(e.z.array(e.z.number()))})),buttons:e.z.optional(e.z.any()),buttonevents:e.z.optional(e.z.any()),items:e.z.array(K(t)),example:e.z.optional(e.z.unknown())})}function K(t){return F(t).omit({$schema:!0,schema:!0,id:!0}).extend({name:e.z.enum(t.attributes,{errorMap:(r,u)=>r.code==="invalid_enum_value"?{message:`Invalid enum value. Expected item from generic attributes definition, received '${r.received}'`}:{message:u.defaultError}}).describe("Item name.")})}function Q(t){return e.z.discriminatedUnion("bind",[e.z.strictObject({bind:e.z.literal("unicast"),"src.ep":v().describe("Source endpoint."),"dst.ep":e.z.optional(v()).describe("Destination endpoint, generaly 0x01."),cl:l(4).describe("Cluster."),report:e.z.optional(e.z.array(e.z.strictObject({at:l(4),dt:l(2),mf:e.z.optional(l(4)),min:e.z.number(),max:e.z.number(),change:e.z.optional(l().or(e.z.number()))}).refine(r=>r.min<=r.max,{message:"invalid report time, min should be smaller than max"})))}),e.z.strictObject({bind:e.z.literal("groupcast"),"src.ep":v().describe("Source endpoint."),cl:l(4).describe("Cluster."),"config.group":e.z.number().min(0).max(255)})])}function ee(t){const r=e.z.array(e.z.enum(t.attributes,{errorMap:(u,i)=>u.code==="invalid_enum_value"?{message:`Invalid enum value. Expected item from generic attributes definition, received '${u.received}'`}:{message:i.defaultError}}));return e.z.strictObject({$schema:e.z.optional(e.z.string()),schema:e.z.literal("subdevice1.schema.json"),ddfvalidate:e.z.optional(e.z.boolean()),type:e.z.union([e.z.enum(Object.keys(t.deviceTypes),{errorMap:(u,i)=>u.code==="invalid_enum_value"?{message:`Invalid enum value. Expected type from generic attributes definition, received '${u.received}'`}:{message:i.defaultError}}),e.z.string().regex(/^(?!\$TYPE_).*/g,"The type start with $TYPE_ but is not present in constants.json")]),name:e.z.string(),restapi:e.z.enum(["/lights","/sensors"]),order:e.z.number(),uuid:S(),items:r,items_optional:e.z.optional(r)})}function O(t){return e.z.discriminatedUnion("schema",[J(t),B(),D(),F(t),ee(t)]).superRefine((r,u)=>{switch(r.schema){case"devcap1.schema.json":w[r.schema].map(i=>i(r,u,t));break;case"constants2.schema.json":w[r.schema].map(i=>i(r,u,t));break}})}function te(t={attributes:[],manufacturers:{},deviceTypes:{},resources:{},subDevices:{}}){let r=O(t);const u=()=>{r=O(t)},i=m=>["constants1.schema.json","constants2.schema.json","resourceitem1.schema.json","subdevice1.schema.json"].includes(m),c=m=>m==="devcap1.schema.json",n=m=>{const s=r.parse(m);switch(s.schema){case"constants1.schema.json":t.manufacturers={...t.manufacturers,...s.manufacturers},t.deviceTypes={...t.deviceTypes,...s["device-types"]};break;case"constants2.schema.json":{Object.keys(s).filter(o=>o.startsWith("$MF_")).forEach(o=>{t.manufacturers[o]=s[o]}),Object.keys(s).filter(o=>o.startsWith("$TYPE_")).forEach(o=>{t.deviceTypes[o]=s[o]});break}case"resourceitem1.schema.json":{if(t.attributes.includes(s.id))throw new Error(`Got duplicate resource item with attribute id '${s.id}'.`);const o=s,p=s.id;delete o.$schema,delete o.schema,delete o.id,t.resources[p]=o,t.attributes.push(p);break}case"subdevice1.schema.json":t.subDevices[s.type]=s,t.subDevices[s.name]=s;break;default:throw new Error(`Got invalid generic file, got data with schema '${s.schema}'.`)}return u(),!0},d=m=>r.parse(m);return{generics:t,loadGeneric:n,validate:d,bulkValidate:(m,s,o={})=>{const p=(h,z)=>{var $,j,E;let x=0,I=Number.POSITIVE_INFINITY,_=h,b=[];for(($=o.onSectionStart)==null||$.call(o,z,h.length);b=[],(j=o.onSectionProgress)==null||j.call(o,z,1,h.length),_.forEach(g=>{var T;(T=o.onSectionProgress)==null||T.call(o,z,x++,h.length);try{z==="generic"?n(g.data):z==="ddf"&&d(g.data)}catch(Z){b.push({...g,error:Z})}}),!(b.length===0||b.length>=I);)x-=b.length,_=b,I=b.length;return(E=o.onSectionEnd)==null||E.call(o,z,h.length,b),b};return[...p(m,"generic"),...p(s,"ddf")]},getSchema:()=>r,version:G,isGeneric:i,isDDF:c}}exports.createValidator=te;
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("zod"),G="2.24.0",w={"devcap1.schema.json":[M,P,W,L,N,Y],"constants2.schema.json":[R]};function M(t,r,u){const i=typeof t.manufacturername=="string"&&typeof t.modelid=="string";if(i)return;const c=Array.isArray(t.manufacturername)&&Array.isArray(t.modelid);if(c&&t.manufacturername.length!==t.modelid.length){r.addIssue({code:e.z.ZodIssueCode.invalid_intersection_types,message:"When 'manufacturername' and 'modelid' are both arrays they should be the same length",path:["manufacturername","modelid"]});return}(i||c)===!1&&r.addIssue({code:e.z.ZodIssueCode.invalid_intersection_types,message:"Invalid properties 'manufacturername' and 'modelid' should have the same type",path:["manufacturername","modelid"]})}function P(t,r,u){if(!t.bindings)return;const i=n=>`0x${(typeof n=="number"?n:Number.parseInt(n,16)).toString(16)}`,c={};t.bindings.forEach(n=>{n.bind==="unicast"&&n.report&&n.report.forEach(d=>{d.max!==65535&&(c[`${i(n["src.ep"])}.${i(n.cl)}.${i(d.at)}`]=d.max)})}),t.subdevices.forEach((n,d)=>{n.items.forEach((a,m)=>{var s;if(a["refresh.interval"]&&a.read&&a.read.fn==="zcl:attr"&&a.read.at){const o=i(a.read.ep??((s=n.fingerprint)==null?void 0:s.endpoint)??n.uuid[1]),p=Array.isArray(a.read.at)?a.read.at:[a.read.at];for(let h=0;h<p.length;h++){const z=`${o}.${i(a.read.cl)}.${i(p[h])}`;c[z]!==void 0&&a["refresh.interval"]-60<c[z]&&r.addIssue({code:e.z.ZodIssueCode.custom,message:`The refresh interval (${a["refresh.interval"]} - 60 = ${a["refresh.interval"]-60}) should be greater than the binding max refresh value (${c[z]}) with a margin of 60 seconds`,path:["subdevices",d,"items",m,"refresh.interval"]})}}})})}function W(t,r,u){const i=[{description:'a device with "state/ct" need the "min" and "max" values for capability.',if:{item:"state/ct"},need:{item:["cap/color/ct/min","cap/color/ct/max"]}},{description:'a device with "cap/color/ct/computes_xy" need the corresponding state/{x,y} items',if:{item:"cap/color/ct/computes_xy"},need:{item:["state/x","state/y","state/ct"]}},{description:'a device with "state/x" or "state/y" need the corresponding cap/color/xy/{red,green,blue}/{x,y} items',if:{item:{or:["state/x","state/y"]}},need:{item:["state/x","state/y","cap/color/xy/red_x","cap/color/xy/green_x","cap/color/xy/blue_x","cap/color/xy/red_y","cap/color/xy/green_y","cap/color/xy/blue_y"]}},{description:'a "(Extended) Color light" need ("state/x" and "state/y") or/and ("state/hue" and "state/sat") .',if:{type:{or:["$TYPE_EXTENDED_COLOR_LIGHT","Extended color light","$TYPE_COLOR_LIGHT","Color light"]}},need:{item:{or:[["state/x","state/y"],["state/hue","state/sat"]]}}}],c=(n,d)=>typeof n=="string"?d(n):Array.isArray(n)?n.every(a=>c(a,d)):"and"in n?n.and.every(a=>c(a,d)):"or"in n?n.or.some(a=>c(a,d)):!1;t.subdevices.forEach((n,d)=>{const a=n.items.map(m=>m.name);i.forEach(m=>{(Object.keys(m.if).length===0||Object.entries(m.if).some(([s,o])=>{switch(s){case"type":return c(o,p=>p===n.type);case"item":return c(o,p=>a.includes(p));default:return!1}}))&&(c(m.need.item,s=>a.includes(s))||r.addIssue({code:e.z.ZodIssueCode.custom,message:`The device is missing some items because ${m.description}`,path:["subdevices",d,"items"]}))})})}function L(t,r,u){const i=["parse","write"];t.subdevices.forEach((c,n)=>{c.items.forEach((d,a)=>{i.forEach(m=>{const s=d[m];s!==void 0&&(s.fn===void 0||s.fn==="zcl:attr"||s.fn==="zcl:cmd")&&(s.eval===void 0&&s.script===void 0&&r.addIssue({code:e.z.ZodIssueCode.custom,message:`The '${m}' function is missing 'eval' or 'script' option.`,path:["subdevices",n,"items",a,m]}),s.eval!==void 0&&s.script!==void 0&&r.addIssue({code:e.z.ZodIssueCode.custom,message:`The '${m}' function is having both 'eval' and 'script' option.`,path:["subdevices",n,"items",a,m]}))})})})}function R(t,r,u){const i=D();Object.keys(t).forEach(c=>{if(!Object.keys(i.shape).includes(c)){if(!["$MF_","$TYPE_"].some(n=>c.startsWith(n))){r.addIssue({code:e.z.ZodIssueCode.custom,message:"The constant key should start with '$MF_' or '$TYPE_'",path:[c]});return}typeof t[c]!="string"&&r.addIssue({code:e.z.ZodIssueCode.custom,message:"The constant value should be a string",path:[c]})}})}function N(t,r,u){t.subdevices.forEach((i,c)=>{const n=u.subDevices[i.type];if(!n){r.addIssue({code:e.z.ZodIssueCode.custom,message:`The device is missing the device definition (subdevice1.schema.json) for the type "${i.type}"`,path:["subdevices",c,"items"]});return}let d=n.items;n.items_optional&&(d=d.filter(a=>!n.items_optional.includes(a))),d.forEach(a=>{i.items.find(m=>m.name===a)||r.addIssue({code:e.z.ZodIssueCode.custom,message:`The device should have the item "${a}" because it is mandatory for devices of type "${i.type}"`,path:["subdevices",c,"items"]})})})}function Y(t,r,u){t.subdevices.forEach((i,c)=>{const n=i.items.map(d=>d.name);i.items.forEach((d,a)=>{d.parse&&d.parse.fn==="numtostr"&&(n.includes(d.parse.srcitem)||r.addIssue({code:e.z.ZodIssueCode.custom,message:`The device should have the item "${d.parse.srcitem}" because it is used in the 'numtostr' function`,path:["subdevices",c,"items",a,"parse","srcitem"]}))})})}function B(t){return e.z.strictObject({$schema:e.z.optional(e.z.string()),schema:e.z.literal("constants1.schema.json"),ddfvalidate:e.z.optional(e.z.boolean()),manufacturers:e.z.record(e.z.string().startsWith("$MF_"),e.z.string()),"device-types":e.z.record(e.z.string().startsWith("$TYPE_"),e.z.string())})}function D(t){return e.z.object({$schema:e.z.optional(e.z.string()),schema:e.z.literal("constants2.schema.json"),ddfvalidate:e.z.optional(e.z.boolean())}).passthrough()}function U(){return e.z.string().regex(/^(\d{4})-(?:(?:0[1-9])|(?:1[0-2]))-(?:(?:0[1-9])|(?:[12][0-9])|(?:3[01]))$/,"Invalid date value")}function l(t=void 0){const r="Invalid hexadecimal value";return t===void 0?e.z.string().regex(/^0x[0-9a-fA-F]+$/,r):e.z.string().regex(new RegExp(`^0x[0-9a-fA-F]{${t}}$`),r)}function v(){return e.z.union([l(2),e.z.number().min(0).max(255)])}function V(){return e.z.custom(t=>{if(!Array.isArray(t)||t.length%2!==0)return!1;for(let r=0;r<t.length;r+=2){const u=t[r],i=t[r+1];if(typeof u!="number"||typeof i!="string")return!1}return!0},"The value must be an array with an even number of values and alternating between number and string.")}function S(){return e.z.union([e.z.tuple([e.z.literal("$address.ext"),l(2)]),e.z.tuple([e.z.literal("$address.ext"),l(2),l(4)])])}function C(){return e.z.string()}function A(){return e.z.string()}const y=e.z.strictObject({ep:e.z.optional(v()).describe("Endpoint, 255 means any endpoint, 0 means auto selected from subdevice."),cl:l(4).describe("Cluster ID."),at:e.z.optional(l(4).or(e.z.array(l(4)))).describe("Attribute ID."),mf:e.z.optional(l(4)).describe("Manufacturer code, must be set to 0x0000 for non manufacturer specific commands."),cmd:e.z.optional(l(2)).describe("Zigbee command."),eval:e.z.optional(A()).describe("Javascript expression to transform the attribute value to the Item value."),script:e.z.optional(C()).describe("Relative path of a Javascript .js file.")}),f={read:y.extend({fc:e.z.optional(l(2).or(e.z.number())).describe("Zigbee command frame control.")}),parse:y.extend({}),write:y.extend({"state.timeout":e.z.optional(e.z.number()),"change.timeout":e.z.optional(e.z.number()),dt:e.z.optional(l(2)).describe("Data type.")})};function k(){return e.z.discriminatedUnion("fn",[e.z.strictObject({fn:e.z.literal("none")}),f.read.extend({fn:e.z.undefined().describe("Generic function to read ZCL attributes.")}),f.read.extend({fn:e.z.literal("zcl:attr").describe("Generic function to parse ZCL values from read/report commands.")}),f.read.extend({fn:e.z.literal("zcl:cmd").describe("Generic function to parse ZCL values from read/report commands.")}),e.z.strictObject({fn:e.z.literal("tuya").describe("Generic function to read all Tuya datapoints. It has no parameters.")})]).refine(t=>!("eval"in t&&"script"in t),{message:"eval and script should not both be present"})}function q(t){return e.z.discriminatedUnion("fn",[f.parse.extend({fn:e.z.undefined().describe("Generic function to read ZCL attributes."),cppsrc:e.z.optional(e.z.string())}),f.parse.extend({fn:e.z.literal("zcl:attr").describe("Generic function to parse ZCL values from read/report commands.")}),f.parse.extend({fn:e.z.literal("zcl:cmd").describe("Generic function to parse ZCL values from read/report commands.")}),e.z.strictObject({fn:e.z.literal("ias:zonestatus").describe("Generic function to parse IAS ZONE status change notifications or zone status from read/report command."),mask:e.z.optional(e.z.enum(["alarm1","alarm2"]).or(e.z.literal("alarm1,alarm2"))).describe("Sets the bitmask for Alert1 and Alert2 item of the IAS Zone status.")}),e.z.strictObject({fn:e.z.literal("numtostr").describe("Generic function to to convert number to string."),srcitem:e.z.enum(t.attributes,{errorMap:(r,u)=>r.code==="invalid_enum_value"?{message:`Invalid enum value. Expected item from generic attributes definition, received '${r.received}'`}:{message:u.defaultError}}).describe("The source item holding the number."),op:e.z.enum(["lt","le","eq","gt","ge"]).describe("Comparison operator (lt | le | eq | gt | ge)"),to:V().describe("Array of (num, string) mappings")}),e.z.strictObject({fn:e.z.literal("time").describe("Specialized function to parse time, local and last set time from read/report commands of the time cluster and auto-sync time if needed.")}),f.parse.pick({ep:!0,at:!0,mf:!0,eval:!0,script:!0}).extend({fn:e.z.literal("xiaomi:special").describe("Generic function to parse custom Xiaomi attributes and commands."),idx:l(2).describe("A 8-bit string hex value.")}),f.parse.pick({eval:!0,script:!0}).extend({fn:e.z.literal("tuya").describe("Generic function to parse Tuya data."),dpid:e.z.number().describe("Data point ID. 1-255 the datapoint ID.")})]).refine(r=>!("eval"in r&&"script"in r),{message:"eval and script should not both be present"})}function H(){return e.z.discriminatedUnion("fn",[e.z.strictObject({fn:e.z.literal("none")}),f.write.extend({fn:e.z.undefined().describe("Generic function to read ZCL attributes."),cppsrc:e.z.optional(e.z.string())}),f.write.extend({fn:e.z.literal("zcl:attr").describe("Generic function to parse ZCL values from read/report commands.")}),f.write.extend({fn:e.z.literal("zcl:cmd").describe("Generic function to parse ZCL values from read/report commands.")}),f.write.pick({eval:!0,script:!0}).extend({fn:e.z.literal("tuya").describe("Generic function to write Tuya data."),dpid:e.z.number().describe("Data point ID. 1-255 the datapoint ID."),dt:l(2).describe("Data type.")})]).refine(t=>!("eval"in t&&"script"in t),{message:"eval and script should not both be present"})}function F(t){return e.z.strictObject({$schema:e.z.optional(e.z.string()),schema:e.z.literal("resourceitem1.schema.json"),id:e.z.string(),ddfvalidate:e.z.optional(e.z.boolean()),description:e.z.optional(e.z.string()).describe("Item description, better to do not use it."),comment:e.z.optional(e.z.string()).describe("TODO: What is this ? What the difference with description ?"),deprecated:e.z.optional(U()).describe("Date of deprecation, if the item is deprecated, it's better to use the new one."),datatype:e.z.optional(e.z.enum(["String","Bool","Int8","Int16","Int32","Int64","UInt8","UInt16","UInt32","UInt64","Double","Array","Array[3]","ISO 8601 timestamp"])).describe("Data type of the item."),access:e.z.optional(e.z.enum(["R","W","RW"])).describe("Access mode for this item, some of them are not editable."),public:e.z.optional(e.z.boolean()).describe("Item visible on the API."),implicit:e.z.optional(e.z.boolean()).describe("TODO: What is this ?"),managed:e.z.optional(e.z.boolean()).describe("TODO: What is this ?"),awake:e.z.optional(e.z.boolean()).describe("The device is considered awake when this item is set due a incoming command."),static:e.z.optional(e.z.union([e.z.string(),e.z.number(),e.z.boolean()])).describe("A static default value is fixed and can be not changed."),range:e.z.optional(e.z.tuple([e.z.number(),e.z.number()])).describe("Values range limit."),virtual:e.z.optional(e.z.boolean()).describe("TODO: What is this ?"),read:e.z.optional(k()).describe("Fonction used to read value."),parse:e.z.optional(q(t)).describe("Fonction used to parse incoming values."),write:e.z.optional(H()).describe("Fonction used to write value."),"refresh.interval":e.z.optional(e.z.number()).describe("Refresh interval used for read fonction, NEED to be superior at value used in binding part."),values:e.z.optional(e.z.unknown()).describe("TODO: What is this ?"),default:e.z.optional(e.z.unknown()).describe("Defaut value.")})}function J(t){return e.z.strictObject({$schema:e.z.optional(e.z.string()),schema:e.z.literal("devcap1.schema.json"),uuid:e.z.optional(e.z.string()),ddfvalidate:e.z.optional(e.z.boolean()),version:e.z.optional(e.z.string()),version_deconz:e.z.optional(e.z.string()),"doc:path":e.z.optional(e.z.string()),"doc:hdr":e.z.optional(e.z.string()),"md:known_issues":e.z.optional(e.z.array(e.z.string())).describe("Know issues for this device, markdown file."),manufacturername:e.z.union([e.z.enum(Object.keys(t.manufacturers)),e.z.string().regex(/^(?!\$MF_).*/g,"The manufacturer name start with $MF_ but is not present in constants.json"),e.z.array(e.z.union([e.z.enum(Object.keys(t.manufacturers)),e.z.string().regex(/^(?!\$MF_).*/g,"The manufacturer name start with $MF_ but is not present in constants.json")]))]).describe("Manufacturer name from Basic Cluster."),modelid:e.z.string().or(e.z.array(e.z.string())).describe("Model ID from Basic Cluster."),vendor:e.z.optional(e.z.string()).describe("Friendly name of the manufacturer."),comment:e.z.optional(e.z.string()),matchexpr:e.z.optional(A()).describe("Need to return true for the DDF be used."),path:e.z.optional(C()).describe("DDF path, useless, can be removed."),product:e.z.optional(e.z.string()).describe("Complements the model id to be shown in the UI."),sleeper:e.z.optional(e.z.boolean()).describe("Sleeping devices can only receive when awake."),supportsMgmtBind:e.z.optional(e.z.boolean()),status:e.z.enum(["Draft","Bronze","Silver","Gold"]).describe("The code quality of the DDF file."),subdevices:e.z.array(X(t)).describe("Devices section."),bindings:e.z.optional(e.z.array(Q())).describe("Bindings section.")})}function X(t){return e.z.strictObject({type:e.z.union([e.z.enum(Object.keys(t.deviceTypes),{errorMap:(r,u)=>r.code==="invalid_enum_value"?{message:`Invalid enum value. Expected type from generic attributes definition, received '${r.received}'`}:{message:u.defaultError}}),e.z.string().regex(/^(?!\$TYPE_).*/g,"The type start with $TYPE_ but is not present in constants.json")]),restapi:e.z.enum(["/lights","/sensors"]),uuid:S(),fingerprint:e.z.optional(e.z.strictObject({profile:l(4),device:l(4),endpoint:v(),in:e.z.optional(e.z.array(l(4))),out:e.z.optional(e.z.array(l(4)))})),meta:e.z.optional(e.z.strictObject({values:e.z.any(),"group.endpoints":e.z.optional(e.z.array(e.z.number()))})),buttons:e.z.optional(e.z.any()),buttonevents:e.z.optional(e.z.any()),items:e.z.array(K(t)),example:e.z.optional(e.z.unknown())})}function K(t){return F(t).omit({$schema:!0,schema:!0,id:!0}).extend({name:e.z.enum(t.attributes,{errorMap:(r,u)=>r.code==="invalid_enum_value"?{message:`Invalid enum value. Expected item from generic attributes definition, received '${r.received}'`}:{message:u.defaultError}}).describe("Item name.")})}function Q(t){return e.z.discriminatedUnion("bind",[e.z.strictObject({bind:e.z.literal("unicast"),"src.ep":v().describe("Source endpoint."),"dst.ep":e.z.optional(v()).describe("Destination endpoint, generaly 0x01."),cl:l(4).describe("Cluster."),report:e.z.optional(e.z.array(e.z.strictObject({at:l(4),dt:l(2),mf:e.z.optional(l(4)),min:e.z.number(),max:e.z.number(),change:e.z.optional(l().or(e.z.number()))}).refine(r=>r.min<=r.max,{message:"invalid report time, min should be smaller than max"})))}),e.z.strictObject({bind:e.z.literal("groupcast"),"src.ep":v().describe("Source endpoint."),cl:l(4).describe("Cluster."),"config.group":e.z.number().min(0).max(255)})])}function ee(t){const r=e.z.array(e.z.enum(t.attributes,{errorMap:(u,i)=>u.code==="invalid_enum_value"?{message:`Invalid enum value. Expected item from generic attributes definition, received '${u.received}'`}:{message:i.defaultError}}));return e.z.strictObject({$schema:e.z.optional(e.z.string()),schema:e.z.literal("subdevice1.schema.json"),ddfvalidate:e.z.optional(e.z.boolean()),type:e.z.union([e.z.enum(Object.keys(t.deviceTypes),{errorMap:(u,i)=>u.code==="invalid_enum_value"?{message:`Invalid enum value. Expected type from generic attributes definition, received '${u.received}'`}:{message:i.defaultError}}),e.z.string().regex(/^(?!\$TYPE_).*/g,"The type start with $TYPE_ but is not present in constants.json")]),name:e.z.string(),restapi:e.z.enum(["/lights","/sensors"]),order:e.z.number(),uuid:S(),items:r,items_optional:e.z.optional(r)})}function O(t){return e.z.discriminatedUnion("schema",[J(t),B(),D(),F(t),ee(t)]).superRefine((r,u)=>{switch(r.schema){case"devcap1.schema.json":w[r.schema].map(i=>i(r,u,t));break;case"constants2.schema.json":w[r.schema].map(i=>i(r,u,t));break}})}function te(t={attributes:[],manufacturers:{},deviceTypes:{},resources:{},subDevices:{}}){let r=O(t);const u=()=>{r=O(t)},i=m=>["constants1.schema.json","constants2.schema.json","resourceitem1.schema.json","subdevice1.schema.json"].includes(m),c=m=>m==="devcap1.schema.json",n=m=>{const s=r.parse(m);switch(s.schema){case"constants1.schema.json":t.manufacturers={...t.manufacturers,...s.manufacturers},t.deviceTypes={...t.deviceTypes,...s["device-types"]};break;case"constants2.schema.json":{Object.keys(s).filter(o=>o.startsWith("$MF_")).forEach(o=>{t.manufacturers[o]=s[o]}),Object.keys(s).filter(o=>o.startsWith("$TYPE_")).forEach(o=>{t.deviceTypes[o]=s[o]});break}case"resourceitem1.schema.json":{if(t.attributes.includes(s.id))throw new Error(`Got duplicate resource item with attribute id '${s.id}'.`);const o=s,p=s.id;delete o.$schema,delete o.schema,delete o.id,t.resources[p]=o,t.attributes.push(p);break}case"subdevice1.schema.json":t.subDevices[s.type]=s,t.subDevices[s.name]=s;break;default:throw new Error(`Got invalid generic file, got data with schema '${s.schema}'.`)}return u(),!0},d=m=>r.parse(m);return{generics:t,loadGeneric:n,validate:d,bulkValidate:(m,s,o={})=>{const p=(h,z)=>{var $,j,E;let x=0,I=Number.POSITIVE_INFINITY,_=h,b=[];for(($=o.onSectionStart)==null||$.call(o,z,h.length);b=[],(j=o.onSectionProgress)==null||j.call(o,z,1,h.length),_.forEach(g=>{var T;(T=o.onSectionProgress)==null||T.call(o,z,x++,h.length);try{z==="generic"?n(g.data):z==="ddf"&&d(g.data)}catch(Z){b.push({...g,error:Z})}}),!(b.length===0||b.length>=I);)x-=b.length,_=b,I=b.length;return(E=o.onSectionEnd)==null||E.call(o,z,h.length,b),b};return[...p(m,"generic"),...p(s,"ddf")]},getSchema:()=>r,version:G,isGeneric:i,isDDF:c}}exports.createValidator=te;
|
package/dist/ddf-validator.d.ts
CHANGED
|
@@ -688,6 +688,8 @@ export declare function createValidator(generics?: GenericsData): {
|
|
|
688
688
|
loadGeneric: (data: unknown) => DDF;
|
|
689
689
|
validate: (data: unknown) => DDF;
|
|
690
690
|
getSchema: () => ZodType<DDF>;
|
|
691
|
+
isGeneric: (schema: string) => boolean;
|
|
692
|
+
isDDF: (schema: string) => boolean;
|
|
691
693
|
bulkValidate: (genericFiles: FileDefinition[], ddfFiles: FileDefinition[], callbacks?: {
|
|
692
694
|
onSectionStart?: ((type: 'generic' | 'ddf', total: number) => void) | undefined;
|
|
693
695
|
onSectionProgress?: ((type: 'generic' | 'ddf', current: number, total: number) => void) | undefined;
|
package/dist/ddf-validator.mjs
CHANGED
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@deconz-community/ddf-validator",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "2.
|
|
5
|
-
"packageManager": "pnpm@8.
|
|
4
|
+
"version": "2.24.0",
|
|
5
|
+
"packageManager": "pnpm@8.12.0",
|
|
6
6
|
"description": "Validating DDF files for deconz",
|
|
7
7
|
"author": {
|
|
8
8
|
"name": "Zehir",
|
|
@@ -60,24 +60,24 @@
|
|
|
60
60
|
},
|
|
61
61
|
"devDependencies": {
|
|
62
62
|
"@types/jsdom": "^21.1.6",
|
|
63
|
-
"@types/node": "^20.10.
|
|
63
|
+
"@types/node": "^20.10.4",
|
|
64
64
|
"@types/pako": "^2.0.3",
|
|
65
|
-
"@typescript-eslint/eslint-plugin": "^6.
|
|
66
|
-
"@typescript-eslint/parser": "^6.
|
|
65
|
+
"@typescript-eslint/eslint-plugin": "^6.14.0",
|
|
66
|
+
"@typescript-eslint/parser": "^6.14.0",
|
|
67
67
|
"degit": "^2.8.4",
|
|
68
68
|
"eslint": "^8.55.0",
|
|
69
69
|
"fast-glob": "^3.3.2",
|
|
70
|
-
"lint-staged": "^15.
|
|
70
|
+
"lint-staged": "^15.2.0",
|
|
71
71
|
"rimraf": "^5.0.5",
|
|
72
|
-
"stylelint": "^
|
|
73
|
-
"stylelint-config-recommended": "^
|
|
72
|
+
"stylelint": "^16.0.2",
|
|
73
|
+
"stylelint-config-recommended": "^14.0.0",
|
|
74
74
|
"stylelint-config-sass-guidelines": "^10.0.0",
|
|
75
75
|
"taze": "^0.13.0",
|
|
76
|
-
"ts-node": "^10.9.
|
|
77
|
-
"typescript": "^5.3.
|
|
78
|
-
"vite": "^5.0.
|
|
79
|
-
"vitest": "^0.
|
|
80
|
-
"zod-to-json-schema": "^3.22.
|
|
76
|
+
"ts-node": "^10.9.2",
|
|
77
|
+
"typescript": "^5.3.3",
|
|
78
|
+
"vite": "^5.0.7",
|
|
79
|
+
"vitest": "^1.0.4",
|
|
80
|
+
"zod-to-json-schema": "^3.22.2",
|
|
81
81
|
"zod-to-ts": "^1.2.0",
|
|
82
82
|
"zod-validation-error": "^2.1.0"
|
|
83
83
|
},
|
|
@@ -89,7 +89,6 @@
|
|
|
89
89
|
"lint": "eslint",
|
|
90
90
|
"test": "vitest run",
|
|
91
91
|
"test:watch": "vitest",
|
|
92
|
-
"test:download-sample": "rimraf ./test-data && degit dresden-elektronik/deconz-rest-plugin /tmp/test-data && mv /tmp/test-data/devices ./test-data && rimraf /tmp/test-data"
|
|
93
|
-
"up": "taze major -I"
|
|
92
|
+
"test:download-sample": "rimraf ./test-data && degit dresden-elektronik/deconz-rest-plugin /tmp/test-data && mv /tmp/test-data/devices ./test-data && rimraf /tmp/test-data"
|
|
94
93
|
}
|
|
95
94
|
}
|