diff --git a/src/formats/json/JsonFormatError.ts b/src/formats/json/JsonFormatError.ts
new file mode 100644
index 0000000000..aa6842d68a
--- /dev/null
+++ b/src/formats/json/JsonFormatError.ts
@@ -0,0 +1,14 @@
+import { AbstractFormatError } from "../../errors/AbstractFormatError";
+
+/**
+ * This error indicates problem with parsing of CSV
+ *
+ * @public exported from `@promptbook/core`
+ */
+export class CsvFormatError extends AbstractFormatError {
+ public readonly name = 'CsvFormatError';
+ public constructor(message: string) {
+ super(message);
+ Object.setPrototypeOf(this, CsvFormatError.prototype);
+ }
+}
diff --git a/src/formats/xml/XmlFormatError.ts b/src/formats/xml/XmlFormatError.ts
new file mode 100644
index 0000000000..a8098bd4c0
--- /dev/null
+++ b/src/formats/xml/XmlFormatError.ts
@@ -0,0 +1,14 @@
+import { AbstractFormatError } from '../../errors/AbstractFormatError';
+
+/**
+ * This error indicates problem with parsing of XML
+ *
+ * @public exported from `@promptbook/core`
+ */
+export class XmlFormatError extends AbstractFormatError {
+ public readonly name = 'XmlFormatError';
+ public constructor(message: string) {
+ super(message);
+ Object.setPrototypeOf(this, CsvFormatError.prototype);
+ }
+}
diff --git a/src/formats/xml/XmlFormatParser.ts b/src/formats/xml/XmlFormatParser.ts
index ec1863ef8f..e1d262f609 100644
--- a/src/formats/xml/XmlFormatParser.ts
+++ b/src/formats/xml/XmlFormatParser.ts
@@ -3,13 +3,20 @@ import { TODO_USE } from '../../utils/organization/TODO_USE';
import type { FormatParser } from '../_common/FormatParser';
import { isValidXmlString } from './utils/isValidXmlString';
+/**
+ * Semantic helper
+ *
+ * For example `"bar"`
+ */
+export type string_xml = string;
+
/**
* Definition for XML format
*
* @private still in development [🏢]
*/
export const XmlFormatParser: FormatParser<
- string /* <- [0] */,
+ string_xml /* <- [0] */,
string /* <- [👨⚖️] */,
TODO_any /* <- [1] */,
TODO_any /* <- [1] */
@@ -18,7 +25,7 @@ export const XmlFormatParser: FormatParser<
mimeType: 'application/xml',
- isValid(value, settings, schema): value is string /* <- [0] */ {
+ isValid(value, settings, schema): value is string_xml /* <- [0] */ {
TODO_USE(value /* <- TODO: Use value here */);
TODO_USE(settings /* <- TODO: Use settings here */);
TODO_USE(schema /* <- TODO: Use schema here */);
diff --git a/src/formats/xml/utils/isValidXmlString.test.ts b/src/formats/xml/utils/isValidXmlString.test.ts
index 35898f09c5..82c8530f95 100644
--- a/src/formats/xml/utils/isValidXmlString.test.ts
+++ b/src/formats/xml/utils/isValidXmlString.test.ts
@@ -27,4 +27,9 @@ describe('isValidXmlString', () => {
expect(isValidXmlString(null as unknown as string)).toBe(false);
expect(isValidXmlString(undefined as unknown as string)).toBe(false);
});
+
+ it('should return false for strings with only whitespace', () => {
+ const whitespaceString = ' ';
+ expect(isValidXmlString(whitespaceString)).toBe(false);
+ });
});
diff --git a/src/formats/xml/utils/isValidXmlString.ts b/src/formats/xml/utils/isValidXmlString.ts
index df628acf4b..3238fbce9f 100644
--- a/src/formats/xml/utils/isValidXmlString.ts
+++ b/src/formats/xml/utils/isValidXmlString.ts
@@ -1,25 +1,19 @@
-import { assertsError } from '../../../errors/assertsError';
+import { string_xml } from '../XmlFormatDefinition';
+import { validateXmlString } from './validateXmlString';
/**
- * Function to check if a string is valid XML
+ * Function to check if a string is valid XML.
*
- * @param value
- * @returns True if the string is a valid XML string, false otherwise
+ * @param value - The string to check.
+ * @returns True if the string is a valid XML string, false otherwise.
*
* @public exported from `@promptbook/utils`
*/
-export function isValidXmlString(value: string): boolean {
+export function isValidXmlString(value: unknown): value is string_xml {
try {
- const parser = new DOMParser();
- const parsedDocument = parser.parseFromString(value, 'application/xml');
- const parserError = parsedDocument.getElementsByTagName('parsererror');
-
- if (parserError.length > 0) {
- return false;
- }
+ validateXmlString(value);
return true;
- } catch (error) {
- assertsError(error);
+ } catch {
return false;
}
}
diff --git a/src/formats/xml/utils/validateXmlString.ts b/src/formats/xml/utils/validateXmlString.ts
new file mode 100644
index 0000000000..a3c776175d
--- /dev/null
+++ b/src/formats/xml/utils/validateXmlString.ts
@@ -0,0 +1,31 @@
+import { assertsError } from '../../../errors/assertsError';
+import { CsvFormatError } from '../../csv/CsvFormatError';
+import { string_xml } from '../XmlFormatDefinition';
+
+/**
+ * Function to validate if a string is a valid XML string.
+ * Throws an error if the string is not valid XML.
+ *
+ * @param value - The string to validate.
+ * @throws Error if the string is not valid XML.
+ *
+ * @public exported from `@promptbook/utils`
+ */
+
+export function validateXmlString(value: unknown): string_xml {
+ if (typeof value !== 'string' || value.trim() === '') {
+ throw new Error('Invalid XML: Input is not a valid string.');
+ }
+ try {
+ const parser = new DOMParser();
+ const parsedDocument = parser.parseFromString(value, 'application/xml');
+ const parserError = parsedDocument.getElementsByTagName('parsererror');
+
+ if (parserError.length > 0) {
+ throw new CsvFormatError('Invalid XML: Parsing error detected.');
+ }
+ } catch (error) {
+ assertsError(error);
+ throw error;
+ }
+}
diff --git a/src/types/typeAliases.ts b/src/types/typeAliases.ts
index 44f33f9bfe..0fb919d36f 100644
--- a/src/types/typeAliases.ts
+++ b/src/types/typeAliases.ts
@@ -223,15 +223,7 @@ export type string_knowledge_source_link = string_url | string_filename;
*/
export type string_html = string;
-/**
- * Semantic helper
- *
- * For example `"bar"`
- *
- *
- * TODO: [🎞️] Probbably use some object-based method for working with XMLs
- */
-export type string_xml = string;
+
/**
* Semantic helper