diff --git a/src/error/codes/index.ts b/src/error/codes/index.ts index de5785a..4be92e2 100644 --- a/src/error/codes/index.ts +++ b/src/error/codes/index.ts @@ -168,6 +168,9 @@ const SKYFLOW_ERROR_CODE = { INVALID_UPSERT: { http_code: 400, message: errorMessages.INVALID_UPSERT }, INVALID_RETURN_TOKEN: { http_code: 400, message: errorMessages.INVALID_RETURN_TOKEN }, + NO_TOKENS_WITH_TOKEN_MODE: { http_code: 400, message: errorMessages.NO_TOKENS_WITH_TOKEN_MODE }, + INSUFFICIENT_TOKENS_PASSED_FOR_TOKEN_MODE_ENABLE_STRICT: { http_code: 400, message: errorMessages.INSUFFICIENT_TOKENS_PASSED_FOR_TOKEN_MODE_ENABLE_STRICT }, + INVALID_DOWNLOAD_URL: { http_code: 400, message: errorMessages.INVALID_DOWNLOAD_URL }, INVALID_FIELD: { http_code: 400, message: errorMessages.INVALID_FIELD }, diff --git a/src/error/messages/index.ts b/src/error/messages/index.ts index 5c102c0..f4f6aed 100644 --- a/src/error/messages/index.ts +++ b/src/error/messages/index.ts @@ -171,6 +171,9 @@ const errorMessages = { INVALID_UPSERT: `${errorPrefix} Validation error. The upsert key has a value of type %s1. Specify upsert as string.`, INVALID_RETURN_TOKEN: `${errorPrefix} Validation error. The returnToken key has a value of type %s1. Specify returnToken as boolean.`, + NO_TOKENS_WITH_TOKEN_MODE: `${errorPrefix} Validation error. Tokens weren't specified for records while 'tokenMode' was ENABLE or ENABLE_STRICT` , + INSUFFICIENT_TOKENS_PASSED_FOR_TOKEN_MODE_ENABLE_STRICT: `${errorPrefix} Validation error. 'tokenMode' is set to 'ENABLE_STRICT', but some fields are missing tokens. Specify tokens for all fields.`, + INVALID_DOWNLOAD_URL: `${errorPrefix} Validation error. The downloadURL key has a value of type %s1. Specify downloadURL as string.`, EMPTY_FIELD: `${errorPrefix} Validation error. Filed value cannot be empty. Specify a valid filed value at index %s1.`, diff --git a/src/utils/validations/index.ts b/src/utils/validations/index.ts index aab2377..09b31ba 100644 --- a/src/utils/validations/index.ts +++ b/src/utils/validations/index.ts @@ -1,4 +1,4 @@ -import { CONNECTION, CONNECTION_ID, Env, isValidURL, LogLevel, MessageType, RequestMethod, OrderByEnum, parameterizedString, printLog, RedactionType, SKYFLOW_ID, VAULT, VAULT_ID } from ".."; +import { CONNECTION, CONNECTION_ID, Env, isValidURL, LogLevel, MessageType, RequestMethod, OrderByEnum, parameterizedString, printLog, RedactionType, SKYFLOW_ID, VAULT, VAULT_ID, TokenMode } from ".."; import { V1BYOT } from "../../ _generated_/rest"; import SkyflowError from "../../error"; import SKYFLOW_ERROR_CODE from "../../error/codes"; @@ -433,7 +433,46 @@ export const validateInsertOptions = (insertOptions?: InsertOptions) => { } }; -export const validateInsertRequest = (insertRequest: InsertRequest, insertOptions?: InsertOptions, logLevel: LogLevel = LogLevel.ERROR) => { +const validateTokensMapWithTokenStrict = ( + data: object, + tokens: object +) => { + const dataKeys = Object.keys(data); + + for (const key of dataKeys) { + if (!tokens.hasOwnProperty(key)) { + throw new SkyflowError(SKYFLOW_ERROR_CODE.INSUFFICIENT_TOKENS_PASSED_FOR_TOKEN_MODE_ENABLE_STRICT); + } + } +}; + +export const validateTokensForInsertRequest = ( + insertRequest?: InsertRequest, + insertOptions?: InsertOptions +) => { + if (insertRequest && insertOptions && insertOptions.getTokenMode()) { + if ( + (insertOptions.getTokenMode() == TokenMode.ENABLE || + insertOptions.getTokenMode() == TokenMode.ENABLE_STRICT) && !insertOptions.getTokens() + ) { + throw new SkyflowError(SKYFLOW_ERROR_CODE.NO_TOKENS_WITH_TOKEN_MODE); + } + + if((insertOptions.getTokenMode() == TokenMode.ENABLE_STRICT) && insertOptions.getTokens()) { + if(insertRequest.data.length!=insertOptions.getTokens()?.length) { + throw new SkyflowError(SKYFLOW_ERROR_CODE.INSUFFICIENT_TOKENS_PASSED_FOR_TOKEN_MODE_ENABLE_STRICT); + } + + if(insertOptions.getTokens()) { + for(let i=0;i { // if (insertRequest) { if (!insertRequest?.tableName || !Object.prototype.hasOwnProperty.call(insertRequest, '_tableName')) { printLog(logs.errorLogs.EMPTY_TABLE_IN_INSERT, MessageType.ERROR, logLevel); @@ -468,6 +507,7 @@ export const validateInsertRequest = (insertRequest: InsertRequest, insertOption validateInsertInput(record, index); }); validateInsertOptions(insertOptions); + validateTokensForInsertRequest(insertRequest, insertOptions) } else { throw new SkyflowError(SKYFLOW_ERROR_CODE.INVALID_INSERT_REQUEST); }