UNPKG

@pix.js/validator

Version:
1 lines 4.02 kB
{"version":3,"sources":["../src/index.ts","../src/dynamic.ts","../src/crc-validator.ts","../src/keys.ts","../src/static.ts"],"sourcesContent":["export { getPixKeyType } from '@pix.js/core'\nexport * from './dynamic'\nexport * from './keys'\nexport * from './static'\n","import { dynamicPixSchema, parseDynamicPix } from '@pix.js/core'\nimport { isValidCrc } from './crc-validator'\nimport { ValidateOptions } from './types'\n\n/**\n * Validates a dynamic PIX QR code payload\n * @param payload The QR code payload string to validate\n * @param options Validation options\n * @param options.throwError If true, throws an error when validation fails. Defaults to false\n * @returns True if the payload is valid, false otherwise\n * @throws {Error} If validation fails and throwError is true\n */\nexport const isValidDynamicQrCode = (payload: string, options: ValidateOptions = { throwError: false }): boolean => {\n if (options.throwError) {\n const parsed = dynamicPixSchema.parse(parseDynamicPix(payload))\n return !!parsed\n }\n\n const parsed = dynamicPixSchema.safeParse(parseDynamicPix(payload))\n const crcValid = isValidCrc(payload)\n return parsed.success && crcValid\n}\n","import { crc16ccitt } from 'crc'\n\n/**\n * Validates the CRC of a PIX QR code\n * @param qrcode - The PIX QR code to validate\n * @returns True if the CRC is valid, false otherwise\n */\nexport const isValidCrc = (qrcode: string): boolean => {\n const payload = qrcode.slice(0, -4)\n const crc = qrcode.slice(-4)\n\n const crcCalculated = crc16ccitt(payload).toString(16).toUpperCase().padStart(4, '0')\n return crcCalculated === crc\n}\n","import { keyDetector } from '@pix.js/core'\n\n/**\n * Validates a PIX key\n * @param key The key to validate\n * @returns True if the key is valid, false otherwise\n */\nexport function isPixKey(key: string): boolean {\n return (\n keyDetector.isCPF(key) ||\n keyDetector.isCNPJ(key) ||\n keyDetector.isEmail(key) ||\n keyDetector.isPhone(key) ||\n keyDetector.isRandom(key)\n )\n}\n","import { parseStaticPix, staticPixSchema } from '@pix.js/core'\nimport { isValidCrc } from './crc-validator'\nimport { ValidateOptions } from './types'\n\n/**\n * Validates a static PIX QR code payload\n * @param payload The QR code payload string to validate\n * @param options Validation options\n * @param options.throwError If true, throws an error when validation fails. Defaults to false\n * @returns True if the payload is valid, false otherwise\n * @throws {Error} If validation fails and throwError is true\n */\nexport const isValidStaticQrCode = (payload: string, options: ValidateOptions = { throwError: false }): boolean => {\n if (options.throwError) {\n const parsed = staticPixSchema.parse(parseStaticPix(payload))\n return !!parsed\n }\n\n const parsed = staticPixSchema.safeParse(parseStaticPix(payload))\n const crcValid = isValidCrc(payload)\n return parsed.success && crcValid\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAAAA,eAA8B;;;ACA9B,kBAAkD;;;ACAlD,iBAA2B;AAOpB,IAAM,aAAa,CAAC,WAA4B;AACrD,QAAM,UAAU,OAAO,MAAM,GAAG,EAAE;AAClC,QAAM,MAAM,OAAO,MAAM,EAAE;AAE3B,QAAM,oBAAgB,uBAAW,OAAO,EAAE,SAAS,EAAE,EAAE,YAAY,EAAE,SAAS,GAAG,GAAG;AACpF,SAAO,kBAAkB;AAC3B;;;ADDO,IAAM,uBAAuB,CAAC,SAAiB,UAA2B,EAAE,YAAY,MAAM,MAAe;AAClH,MAAI,QAAQ,YAAY;AACtB,UAAMC,UAAS,6BAAiB,UAAM,6BAAgB,OAAO,CAAC;AAC9D,WAAO,CAAC,CAACA;AAAA,EACX;AAEA,QAAM,SAAS,6BAAiB,cAAU,6BAAgB,OAAO,CAAC;AAClE,QAAM,WAAW,WAAW,OAAO;AACnC,SAAO,OAAO,WAAW;AAC3B;;;AErBA,IAAAC,eAA4B;AAOrB,SAAS,SAAS,KAAsB;AAC7C,SACE,yBAAY,MAAM,GAAG,KACrB,yBAAY,OAAO,GAAG,KACtB,yBAAY,QAAQ,GAAG,KACvB,yBAAY,QAAQ,GAAG,KACvB,yBAAY,SAAS,GAAG;AAE5B;;;ACfA,IAAAC,eAAgD;AAYzC,IAAM,sBAAsB,CAAC,SAAiB,UAA2B,EAAE,YAAY,MAAM,MAAe;AACjH,MAAI,QAAQ,YAAY;AACtB,UAAMC,UAAS,6BAAgB,UAAM,6BAAe,OAAO,CAAC;AAC5D,WAAO,CAAC,CAACA;AAAA,EACX;AAEA,QAAM,SAAS,6BAAgB,cAAU,6BAAe,OAAO,CAAC;AAChE,QAAM,WAAW,WAAW,OAAO;AACnC,SAAO,OAAO,WAAW;AAC3B;","names":["import_core","parsed","import_core","import_core","parsed"]}