UNPKG

@strapi/utils

Version:

Shared utilities for the Strapi packages

1 lines 2.13 kB
{"version":3,"file":"throw-restricted-fields.mjs","sources":["../../../src/validate/visitors/throw-restricted-fields.ts"],"sourcesContent":["import { isArray, isString } from 'lodash/fp';\nimport type { Visitor } from '../../traverse/factory';\nimport { throwInvalidKey } from '../utils';\n\nexport default (restrictedFields: string[] | null = null): Visitor =>\n ({ key, path: { attribute: path } }) => {\n // all fields\n if (restrictedFields === null) {\n throwInvalidKey({ key, path });\n }\n\n // Throw on invalid formats\n if (!(isArray(restrictedFields) && restrictedFields.every(isString))) {\n throw new TypeError(\n `Expected array of strings for restrictedFields but got \"${typeof restrictedFields}\"`\n );\n }\n\n // if an exact match was found\n if (restrictedFields.includes(path as string)) {\n throwInvalidKey({ key, path });\n }\n\n // nested matches\n const isRestrictedNested = restrictedFields.some((allowedPath) =>\n path?.toString().startsWith(`${allowedPath}.`)\n );\n if (isRestrictedNested) {\n throwInvalidKey({ key, path });\n }\n };\n"],"names":["restrictedFields","key","path","attribute","throwInvalidKey","isArray","every","isString","TypeError","includes","isRestrictedNested","some","allowedPath","toString","startsWith"],"mappings":";;;AAIA,4BAAe,CAAA,CAACA,gBAAoC,GAAA,IAAI,GACtD,CAAC,EAAEC,GAAG,EAAEC,IAAM,EAAA,EAAEC,SAAWD,EAAAA,IAAI,EAAE,EAAE,GAAA;;AAEjC,QAAA,IAAIF,qBAAqB,IAAM,EAAA;YAC7BI,eAAgB,CAAA;AAAEH,gBAAAA,GAAAA;AAAKC,gBAAAA;AAAK,aAAA,CAAA;AAC9B;;QAGA,IAAI,EAAEG,OAAQL,CAAAA,gBAAAA,CAAAA,IAAqBA,iBAAiBM,KAAK,CAACC,SAAQ,CAAI,EAAA;YACpE,MAAM,IAAIC,UACR,CAAC,wDAAwD,EAAE,OAAOR,gBAAAA,CAAiB,CAAC,CAAC,CAAA;AAEzF;;QAGA,IAAIA,gBAAAA,CAAiBS,QAAQ,CAACP,IAAiB,CAAA,EAAA;YAC7CE,eAAgB,CAAA;AAAEH,gBAAAA,GAAAA;AAAKC,gBAAAA;AAAK,aAAA,CAAA;AAC9B;;AAGA,QAAA,MAAMQ,kBAAqBV,GAAAA,gBAAAA,CAAiBW,IAAI,CAAC,CAACC,WAAAA,GAChDV,IAAMW,EAAAA,QAAAA,EAAAA,CAAWC,UAAW,CAAA,CAAC,EAAEF,WAAAA,CAAY,CAAC,CAAC,CAAA,CAAA;AAE/C,QAAA,IAAIF,kBAAoB,EAAA;YACtBN,eAAgB,CAAA;AAAEH,gBAAAA,GAAAA;AAAKC,gBAAAA;AAAK,aAAA,CAAA;AAC9B;AACF,KAAA;;;;"}