Skip to content
Open
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
14 changes: 14 additions & 0 deletions src/formats/json/JsonFormatError.ts
Original file line number Diff line number Diff line change
@@ -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);
}
}
14 changes: 14 additions & 0 deletions src/formats/xml/XmlFormatError.ts
Original file line number Diff line number Diff line change
@@ -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);

Check failure on line 12 in src/formats/xml/XmlFormatError.ts

View workflow job for this annotation

GitHub Actions / 🧪 Test types

Cannot find name 'CsvFormatError'.
}
}
11 changes: 9 additions & 2 deletions src/formats/xml/XmlFormatParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 `"<foo>bar</foo>"`
*/
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] */
Expand All @@ -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 */);
Expand Down
5 changes: 5 additions & 0 deletions src/formats/xml/utils/isValidXmlString.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
22 changes: 8 additions & 14 deletions src/formats/xml/utils/isValidXmlString.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,19 @@
import { assertsError } from '../../../errors/assertsError';
import { string_xml } from '../XmlFormatDefinition';

Check failure on line 1 in src/formats/xml/utils/isValidXmlString.ts

View workflow job for this annotation

GitHub Actions / 🧪 Test types

Cannot find module '../XmlFormatDefinition' or its corresponding type declarations.

Check failure on line 1 in src/formats/xml/utils/isValidXmlString.ts

View workflow job for this annotation

GitHub Actions / 🧪✨ Test Books

Cannot find module '../XmlFormatDefinition' or its corresponding type declarations.
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;
}
}
31 changes: 31 additions & 0 deletions src/formats/xml/utils/validateXmlString.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { assertsError } from '../../../errors/assertsError';
import { CsvFormatError } from '../../csv/CsvFormatError';
import { string_xml } from '../XmlFormatDefinition';

Check failure on line 3 in src/formats/xml/utils/validateXmlString.ts

View workflow job for this annotation

GitHub Actions / 🧪 Test types

Cannot find module '../XmlFormatDefinition' or its corresponding type declarations.

/**
* 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;
}
}
10 changes: 1 addition & 9 deletions src/types/typeAliases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,15 +223,7 @@ export type string_knowledge_source_link = string_url | string_filename;
*/
export type string_html = string;

/**
* Semantic helper
*
* For example `"<foo>bar</foo>"`
*
*
* TODO: [🎞️] Probbably use some object-based method for working with XMLs
*/
export type string_xml = string;


/**
* Semantic helper
Expand Down
Loading