Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SK-1806 add validation for enable strict #172

Merged
merged 1 commit into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/error/codes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down
3 changes: 3 additions & 0 deletions src/error/messages/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.`,
Expand Down
44 changes: 42 additions & 2 deletions src/utils/validations/index.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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<insertRequest.data.length;i++) {
validateTokensMapWithTokenStrict(insertRequest.data[i], insertOptions.getTokens()![i])
}
}
}
}
};

export const validateInsertRequest = (insertRequest: InsertRequest, insertOptions?: InsertOptions, logLevel: LogLevel = LogLevel.ERROR) => { //
if (insertRequest) {
if (!insertRequest?.tableName || !Object.prototype.hasOwnProperty.call(insertRequest, '_tableName')) {
printLog(logs.errorLogs.EMPTY_TABLE_IN_INSERT, MessageType.ERROR, logLevel);
Expand Down Expand Up @@ -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);
}
Expand Down
Loading