Skip to content

Commit 8724b9a

Browse files
SK-2134 include file path and file object in request response
* SK-2134 include file path and file object in request response (#232) * [AUTOMATED] Private Release 2.1.0-beta.1-dev.981921e * SK-2134 include file path and file object in request response * [AUTOMATED] Private Release 2.1.0-beta.1-dev.84fb37c * SK-2134 throw error when both file and filepath are provided (#234) * [AUTOMATED] Private Release 2.1.0-beta.1-dev.58e3761 * SK-2134 change error message and file input type * SK-2134 change error message and file input type * [AUTOMATED] Private Release 2.1.0-beta.1-dev.956e5c7 --------- Co-authored-by: skyflow-shravan <skyflow-shravan@users.noreply.github.com>
1 parent 5d729ac commit 8724b9a

11 files changed

Lines changed: 201 additions & 73 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "skyflow-node",
3-
"version": "2.1.0-beta.1",
3+
"version": "2.1.0-beta.1-dev.956e5c7",
44
"description": "Skyflow SDK for Node.js",
55
"main": "./lib/index.js",
66
"module": "./lib/index.js",

src/error/codes/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const SKYFLOW_ERROR_CODE = {
1616
INVALID_BEARER_TOKEN: { http_code: 400, message: errorMessages.INVALID_BEARER_TOKEN },
1717
INVALID_PARSED_CREDENTIALS_STRING: { http_code: 400, message: errorMessages.INVALID_PARSED_CREDENTIALS_STRING },
1818
INVALID_API_KEY: { http_code: 400, message: errorMessages.INVALID_API_KEY },
19-
INVALID_FILE_PATH: { http_code: 400, message: errorMessages.INVALID_FILE_PATH },
19+
INVALID_CREDENTIALS_FILE_PATH: { http_code: 400, message: errorMessages.INVALID_CREDENTIALS_FILE_PATH },
2020

2121
INVALID_BEARER_TOKEN_WITH_ID: { http_code: 400, message: errorMessages.INVALID_BEARER_TOKEN_WITH_ID },
2222
INVALID_PARSED_CREDENTIALS_STRING_WITH_ID: { http_code: 400, message: errorMessages.INVALID_PARSED_CREDENTIALS_STRING_WITH_ID },
@@ -204,6 +204,7 @@ const SKYFLOW_ERROR_CODE = {
204204
INVALID_PLAIN_TEXT_ENTITIES_IN_REIDENTIFY: { http_code: 400, message: errorMessages.INVALID_PLAIN_TEXT_ENTITIES_IN_REIDENTIFY },
205205

206206
INVALID_DEIDENTIFY_FILE_REQUEST: { http_code: 400, message: errorMessages.INVALID_DEIDENTIFY_FILE_REQUEST },
207+
INVALID_DEIDENTIFY_FILE_INPUT: { http_code: 400, message: errorMessages.INVALID_DEIDENTIFY_FILE_INPUT },
207208
EMPTY_FILE_OBJECT:{ http_code: 400, message: errorMessages.EMPTY_FILE_OBJECT },
208209
INVALID_FILE_FORMAT: { http_code: 400, message: errorMessages.INVALID_FILE_FORMAT },
209210
MISSING_FILE_SOURCE: { http_code: 400, message: errorMessages.MISSING_FILE_SOURCE },
@@ -220,6 +221,7 @@ const SKYFLOW_ERROR_CODE = {
220221
INVALID_BLEEP:{ http_code: 400, message: errorMessages.INVALID_BLEEP },
221222
INVALID_FILE_OR_ENCODED_FILE:{ http_code: 400, message: errorMessages.INVALID_FILE_OR_ENCODED_FILE },
222223
INVALID_FILE_TYPE:{ http_code: 400, message: errorMessages.INVALID_FILE_TYPE },
224+
INVALID_DEIDENTIFY_FILE_PATH:{ http_code: 400, message: errorMessages.INVALID_DEIDENTIFY_FILE_PATH },
223225
FILE_READ_ERROR:{ http_code: 400, message: errorMessages.FILE_READ_ERROR },
224226
INVALID_BASE64_HEADER:{ http_code: 400, message: errorMessages.INVALID_BASE64_HEADER },
225227
INVALID_WAIT_TIME:{ http_code: 400, message: errorMessages.INVALID_WAIT_TIME },

src/error/messages/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const errorMessages = {
2121
EMPTY_CREDENTIAL_FILE_PATH: `${errorPrefix} Initialization failed. Invalid credentials. Specify a valid file path.`,
2222
INVALID_CREDENTIAL_FILE_PATH: `${errorPrefix} Initialization failed. Invalid credentials. Expected file path to be a string.`,
2323

24-
INVALID_FILE_PATH: `${errorPrefix} Initialization failed. Invalid skyflow credentials. Expected file path to exists.`,
24+
INVALID_CREDENTIALS_FILE_PATH: `${errorPrefix} Initialization failed. Invalid skyflow credentials. Expected file path to exists.`,
2525
INVALID_API_KEY: `${errorPrefix} Initialization failed. Invalid skyflow credentials. Specify a valid api key.`,
2626
INVALID_PARSED_CREDENTIALS_STRING: `${errorPrefix} Initialization failed. Invalid skyflow credentials. Specify a valid credentials string.`,
2727
INVALID_BEARER_TOKEN: `${errorPrefix} Initialization failed. Invalid skyflow credentials. Specify a valid token.`,
@@ -210,6 +210,7 @@ const errorMessages = {
210210
INVALID_PLAIN_TEXT_ENTITIES_IN_REIDENTIFY: `${errorPrefix} Validation error. The plainTextEntities field must be an array of DetectEntities enums. Specify a valid plainTextEntities.`,
211211

212212
INVALID_DEIDENTIFY_FILE_REQUEST: `${errorPrefix} Validation error. Invalid deidentify file request. Specify a valid deidentify file request.`,
213+
INVALID_DEIDENTIFY_FILE_INPUT: `${errorPrefix} Validation error. Invalid deidentify file input. Please provide either a file or a file path.`,
213214
EMPTY_FILE_OBJECT: `${errorPrefix} Validation error. File object cannot be empty. Specify a valid file object.`,
214215
INVALID_FILE_FORMAT: `${errorPrefix} Validation error. Invalid file format. Specify a valid file format.`,
215216
MISSING_FILE_SOURCE: `${errorPrefix} Validation error. Provide exactly one of filePath, base64, or fileObject.`,
@@ -232,6 +233,7 @@ const errorMessages = {
232233
INVALID_BLEEP: `${errorPrefix} Validation error. Invalid bleep. Specify valid bleep as object.`,
233234
INVALID_FILE_OR_ENCODED_FILE: `${errorPrefix} . Error while decoding base64 and saving file`,
234235
INVALID_FILE_TYPE : `${errorPrefix} Validation error. Invalid file type. Specify a valid file type.`,
236+
INVALID_DEIDENTIFY_FILE_PATH: `${errorPrefix} Validation error. Invalid file path. Specify a valid file path.`,
235237
INVALID_FILE_NAME: `${errorPrefix} Validation error. Invalid file name. Specify a valid file name.`,
236238
FILE_READ_ERROR: `${errorPrefix} Validation error. Unable to read file. Verify the file path.`,
237239
INVALID_BASE64_HEADER: `${errorPrefix} Validation error. Invalid base64 header. Specify a valid base64 header.`,

src/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import UpdateResponse from './vault/model/response/update';
2828
import FileUploadResponse from './vault/model/response/file-upload';
2929
import QueryResponse from './vault/model/response/query';
3030
import InvokeConnectionResponse from './vault/model/response/invoke/invoke';
31-
import { SkyflowConfig, TokenizeRequestType, DetokenizeData, InsertResponseType, GetResponseData, QueryResponseType, IndexRange } from './vault/types';
31+
import { SkyflowConfig, TokenizeRequestType, DetokenizeData, InsertResponseType, GetResponseData, QueryResponseType, IndexRange, FileInput, StringKeyValueMapType } from './vault/types';
3232
import VaultConfig from './vault/config/vault';
3333
import SkyflowError from './error';
3434
import ConnectionConfig from './vault/config/connection';
@@ -119,5 +119,7 @@ export {
119119
InsertResponseType,
120120
GetResponseData,
121121
QueryResponseType,
122-
IndexRange
122+
IndexRange,
123+
StringKeyValueMapType,
124+
FileInput
123125
};

src/utils/validations/index.ts

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ export const validateSkyflowCredentials = (credentials: Credentials, logLevel: L
290290
const pathCred = credentials as PathCredentials;
291291
if (typeof pathCred.path !== 'string' || !isValidPath(pathCred.path)) {
292292
printLog(logs.errorLogs.EMPTY_CREDENTIALS_PATH, MessageType.ERROR, logLevel);
293-
throw new SkyflowError(SKYFLOW_ERROR_CODE.INVALID_FILE_PATH);
293+
throw new SkyflowError(SKYFLOW_ERROR_CODE.INVALID_CREDENTIALS_FILE_PATH);
294294
}
295295
if (pathCred.roles !== undefined && !Array.isArray(pathCred.roles)) {
296296
throw new SkyflowError(SKYFLOW_ERROR_CODE.INVALID_ROLES_KEY_TYPE);
@@ -1057,25 +1057,37 @@ export const validateDeidentifyFileRequest = (deidentifyFileRequest: DeidentifyF
10571057
throw new SkyflowError(SKYFLOW_ERROR_CODE.INVALID_DEIDENTIFY_FILE_REQUEST);
10581058
}
10591059

1060-
// Validate file object
1061-
const file = deidentifyFileRequest.getFile();
1060+
const fileType = deidentifyFileRequest.getFile();
10621061

1063-
// Check if the file is a valid File (browser) or Buffer (Node.js)
1064-
if (!(file instanceof File)) {
1065-
throw new SkyflowError(SKYFLOW_ERROR_CODE.INVALID_FILE_TYPE);
1062+
const hasFile = 'file' in fileType && fileType.file;
1063+
const hasFilePath = 'filePath' in fileType && fileType.filePath;
1064+
1065+
// Must provide exactly one of file or filePath
1066+
if ((hasFile && hasFilePath) || (!hasFile && !hasFilePath)) {
1067+
throw new SkyflowError(SKYFLOW_ERROR_CODE.INVALID_DEIDENTIFY_FILE_INPUT);
10661068
}
10671069

1068-
// Additional validation for File
1069-
if (file instanceof File) {
1070+
if (hasFile) {
1071+
const file = fileType.file;
1072+
if (!(file instanceof File)) {
1073+
throw new SkyflowError(SKYFLOW_ERROR_CODE.INVALID_FILE_TYPE);
1074+
}
10701075
if (!file.name || typeof file.name !== 'string' || file.name.trim().length === 0) {
10711076
throw new SkyflowError(SKYFLOW_ERROR_CODE.INVALID_FILE_TYPE);
10721077
}
1073-
// Validate fileBaseName
10741078
const fileBaseName = path.parse(file.name).name;
10751079
if (!fileBaseName || typeof fileBaseName !== 'string' || fileBaseName.trim().length === 0) {
10761080
throw new SkyflowError(SKYFLOW_ERROR_CODE.INVALID_FILE_TYPE);
10771081
}
1078-
}
1082+
} else if (hasFilePath) {
1083+
const filePath = fileType.filePath;
1084+
if (typeof filePath !== 'string' || filePath.trim().length === 0) {
1085+
throw new SkyflowError(SKYFLOW_ERROR_CODE.INVALID_DEIDENTIFY_FILE_PATH);
1086+
}
1087+
if (!fs.existsSync(filePath) || !fs.lstatSync(filePath).isFile()) {
1088+
throw new SkyflowError(SKYFLOW_ERROR_CODE.INVALID_DEIDENTIFY_FILE_PATH);
1089+
}
1090+
}
10791091

10801092
// Validate options if provided
10811093
if (deidentifyFileOptions) {

0 commit comments

Comments
 (0)