@flatfile/plugin-constraints
Version:
A plugin for extending blueprint with external constraints
11 lines (8 loc) • 1.5 kB
JavaScript
;
var api = require('@flatfile/api');
var pluginRecordHook = require('@flatfile/plugin-record-hook');
function p(r,o){return r.fields.reduce((t,e)=>(e.constraints?.forEach(i=>{i.type==="external"&&i.validator===o&&t.push([e,i]);}),t),[])}function m(r,o){return r.constraints?.filter(t=>t.type==="external"&&t.validator===o)||[]}var h=new api.FlatfileClient,P=(r,o)=>t=>{t.on("commit:created",async e=>{await e.cache.init("sheet-schema",async()=>(await h.sheets.get(e.context.sheetId)).data.config);}),t.use(pluginRecordHook.bulkRecordHook("**",async(e,i)=>{let l=i.cache.get("sheet-schema"),f=p(l,r);for(let[n,s]of f)for(let a of e)try{await o(a.get(n.key),n.key,{config:s.config,record:a,property:n,event:i});}catch(c){a.addError(n.key,String(c));}}));};var C=new api.FlatfileClient,H=(r,o)=>t=>{t.on("commit:created",async e=>{await e.cache.init("sheet-schema",async()=>(await C.sheets.get(e.context.sheetId)).data.config);}),t.use(pluginRecordHook.bulkRecordHook("**",async(e,i)=>{let l=i.cache.get("sheet-schema"),f=m(l,r);for(let n of f){let s=n.fields||[];for(let a of e)try{await o(R(a,s),n.fields,{config:n.config,record:a,properties:x(l,s),event:i});}catch(c){s.forEach(d=>{a.addError(d,String(c));});}}}));};function x(r,o=[]){return r.fields.filter(t=>o.includes(t.key))}function R(r,o){return o.reduce((t,e)=>(t[e]=r.get(e),t),{})}
exports.externalConstraint = P;
exports.externalSheetConstraint = H;
//# sourceMappingURL=index.cjs.map
//# sourceMappingURL=index.cjs.map