{"version":3,"sources":["../../src/schema/fields.js"],"names":["fieldErrors","AddFieldValidationFailed","allowedTypes","all","getNewField","type","id","name","typeOptions","label","getInitialValue","getUndefinedValue","fieldRules","allFields","f","t","typeOptionsRules","field","def","optName","optionDefinitions","keys","o","isValid","requirementDescription","validateField","everySingleField","validateAllFields","recordNode","fields","flatten","addField","recordTemplate","validationMessages","length","errors","m","error","BadRequestError","join","push"],"mappings":"uZAAA;AACA;;;;;;;AAOA;AACA;AACA;AACA;;AAEO,IAAMA,WAAW,GAAG;AACzBC,EAAAA,wBAAwB,EAAE,wBADD,EAApB,C;;;AAIA,IAAMC,YAAY,GAAG,SAAfA,YAAe,WAAM,cAAKC,UAAL,CAAN,EAArB,C;;AAEA,IAAMC,WAAW,GAAG,SAAdA,WAAc,CAAAC,IAAI,UAAK;AAClCC,IAAAA,EAAE,EAAE,wBAD8B;AAElCC,IAAAA,IAAI,EAAE,EAF4B,EAExB;AACVF,IAAAA,IAAI,EAAJA,IAHkC;AAIlCG,IAAAA,WAAW,EAAE,8BAAkBH,IAAlB,CAJqB;AAKlCI,IAAAA,KAAK,EAAE,EAL2B,EAKvB;AACXC,IAAAA,eAAe,EAAE,SANiB,EAMN;AAC5BC,IAAAA,iBAAiB,EAAE,SAPe,CAOJ;AAPI,GAAL,EAAxB,C;;AAUP,IAAMC,UAAU,GAAG,SAAbA,UAAa,CAAAC,SAAS,UAAI;AAC9B,kCAAS,MAAT,EAAiB,uBAAjB,EAA0C,UAAAC,CAAC,UAAI,8BAAiBA,CAAC,CAACP,IAAnB,CAAJ,EAA3C,CAD8B;AAE9B,kCAAS,MAAT,EAAiB,uBAAjB,EAA0C,UAAAO,CAAC,UAAI,8BAAiBA,CAAC,CAACT,IAAnB,CAAJ,EAA3C,CAF8B;AAG9B,kCAAS,OAAT,EAAkB,wBAAlB,EAA4C,UAAAS,CAAC,UAAI,8BAAiBA,CAAC,CAACL,KAAnB,CAAJ,EAA7C,CAH8B;AAI9B,kCAAS,iBAAT,EAA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,EAAN;;AAGD;AACDP,EAAAA,cAAc,CAACH,MAAf,CAAsBW,IAAtB,CAA2BvB,KAA3B;AACD,CAdM,C","sourcesContent":["import { some, map, filter, keys, includes, countBy, flatten } from \"lodash/fp\"\nimport {\n isSomething,\n $,\n isNonEmptyString,\n isNothingOrEmpty,\n isNothing,\n} from \"../common\"\nimport { all, getDefaultOptions } from \"./types/index.js\"\nimport { applyRuleSet, makerule } from \"../common/validationCommon\"\nimport { BadRequestError } from \"../common/errors\"\nimport { generate } from \"shortid\"\n\nexport const fieldErrors = {\n AddFieldValidationFailed: \"Add field validation: \",\n}\n\nexport const allowedTypes = () => keys(all)\n\nexport const getNewField = type => ({\n id: generate(),\n name: \"\", // how field is referenced internally\n type,\n typeOptions: getDefaultOptions(type),\n label: \"\", // how field is displayed\n getInitialValue: \"default\", // function that gets value when initially created\n getUndefinedValue: \"default\", // function that gets value when field undefined on record\n})\n\nconst fieldRules = allFields => [\n makerule(\"name\", \"field name is not set\", f => isNonEmptyString(f.name)),\n makerule(\"type\", \"field type is not set\", f => isNonEmptyString(f.type)),\n makerule(\"label\", \"field label is not set\", f => isNonEmptyString(f.label)),\n makerule(\"getInitialValue\", \"getInitialValue function is not set\", f =>\n isNonEmptyString(f.getInitialValue)\n ),\n makerule(\"getUndefinedValue\", \"getUndefinedValue function is not set\", f =>\n isNonEmptyString(f.getUndefinedValue)\n ),\n makerule(\n \"name\",\n \"field name is duplicated\",\n f => isNothingOrEmpty(f.name) || countBy(\"name\")(allFields)[f.name] === 1\n ),\n makerule(\n \"type\",\n \"type is unknown\",\n f => isNothingOrEmpty(f.type) || some(t => f.type === t)(allowedTypes())\n ),\n]\n\nconst typeOptionsRules = field => {\n const type = all[field.type]\n if (isNothing(type)) return []\n\n const def = optName => type.optionDefinitions[optName]\n\n return $(field.typeOptions, [\n keys,\n filter(o => isSomething(def(o)) && isSomething(def(o).isValid)),\n map(o =>\n makerule(`typeOptions.${o}`, `${def(o).requirementDescription}`, field =>\n def(o).isValid(field.typeOptions[o])\n )\n ),\n ])\n}\n\nexport const validateField = allFields => field => {\n const everySingleField = includes(field)(allFields)\n ? allFields\n : [...allFields, field]\n return applyRuleSet([\n ...fieldRules(everySingleField),\n ...typeOptionsRules(field),\n ])(field)\n}\n\nexport const validateAllFields = recordNode =>\n $(recordNode.fields, [map(validateField(recordNode.fields)), flatten])\n\nexport const addField = (recordTemplate, field) => {\n if (isNothingOrEmpty(field.label)) {\n field.label = field.name\n }\n const validationMessages = validateField([...recordTemplate.fields, field])(\n field\n )\n if (validationMessages.length > 0) {\n const errors = map(m => m.error)(validationMessages)\n throw new BadRequestError(\n `${fieldErrors.AddFieldValidationFailed} ${errors.join(\", \")}`\n )\n }\n recordTemplate.fields.push(field)\n}\n"],"file":"fields.js"}