Skip to content

Commit

Permalink
fix: xml validation (#83)
Browse files Browse the repository at this point in the history
* fix: xml validation

* chore: npm publish automation

---------

Co-authored-by: Alisson Marques Alvarenga <alissonmbr@Alissons-MacBook-Air.local>
  • Loading branch information
alissonmbr and Alisson Marques Alvarenga authored Oct 21, 2024
1 parent c106c3d commit c0601c3
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 8 deletions.
20 changes: 20 additions & 0 deletions .github/workflows/npm-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: Publish Package to npmjs
on:
release:
types: [published]
jobs:
build:
runs-on: ubuntu-latest
permissions:
contents: read
id-token: write
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: '20.x'
registry-url: 'https://registry.npmjs.org'
- run: npm ci
- run: npm publish --provenance --access public
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
20 changes: 20 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
## 2.0.3
_Oct 20, 2024_

- fix: xml validation
- chore: added source map
- chore: updated `fast-xml-parser` to 4.5.0

## 2.0.2

_Oct 14, 2024_

- fix: exported types on package.json

## 2.0.1

_Oct 25, 2023_

- fix: set main to the umd file
- fix: wrong link to the example image

## 2.0.0

_Jun 22, 2023_
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-xml-viewer",
"version": "2.0.2",
"version": "2.0.3",
"description": "Simple xml viewer component for React",
"author": "alissonmbr",
"license": "MIT",
Expand Down Expand Up @@ -83,7 +83,7 @@
"dist"
],
"dependencies": {
"fast-xml-parser": "^4.2.5",
"fast-xml-parser": "^4.5.0",
"lodash": "^4.17.21"
}
}
2 changes: 1 addition & 1 deletion src/components/__tests__/XMLViewer.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ describe('XMLViewer', () => {
});

it('should render invalid xml', () => {
const xml = '<?xml';
const xml = '!';
render(<XMLViewer xml={xml} />);
expect(screen.getByText('Invalid XML!')).toBeInTheDocument();
});
Expand Down
11 changes: 10 additions & 1 deletion src/hooks/useXMLViewer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ATTRIBUTE_CDATA, ATTRIBUTE_COMMENT } from 'contants';
import { XMLParser } from 'fast-xml-parser';
import { XMLParser, XMLValidator } from 'fast-xml-parser';
import { useMemo } from 'react';

const parser = new XMLParser({
Expand All @@ -15,7 +15,16 @@ const parser = new XMLParser({
export default function useXMLViewer(xml: string) {
return useMemo(() => {
try {
if (!XMLValidator.validate(xml)) {
throw new Error('Invalid XML!');
}

const json = parser.parse(xml);

if (typeof xml === 'string' && xml.trim().length > 0 && json.length === 0) {
throw new Error('Invalid XML!');
}

return { json, valid: true };
} catch (e) {
const error = e as Error;
Expand Down
1 change: 1 addition & 0 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export default defineConfig({
tsconfigPaths(),
],
build: {
sourcemap: true,
lib: {
entry: path.resolve(__dirname, 'src/index.ts'),
name: 'ReactXmlViewer',
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5610,10 +5610,10 @@ fast-levenshtein@^2.0.6, fast-levenshtein@~2.0.6:
resolved "https://registry.yarnpkg.com/fast-levenshtein/-/fast-levenshtein-2.0.6.tgz#3d8a5c66883a16a30ca8643e851f19baa7797917"
integrity sha512-DCXu6Ifhqcks7TZKY3Hxp3y6qphY5SJZmrWMDrKcERSOXWQdMhU9Ig/PYrzyw/ul9jOIyh0N4M0tbC5hodg8dw==

fast-xml-parser@^4.2.5:
version "4.2.5"
resolved "https://registry.yarnpkg.com/fast-xml-parser/-/fast-xml-parser-4.2.5.tgz#a6747a09296a6cb34f2ae634019bf1738f3b421f"
integrity sha512-B9/wizE4WngqQftFPmdaMYlXoJlJOYxGQOanC77fq9k8+Z0v5dDSVh+3glErdIROP//s/jgb7ZuxKfB8nVyo0g==
fast-xml-parser@^4.5.0:
version "4.5.0"
resolved "https://registry.yarnpkg.com/fast-xml-parser/-/fast-xml-parser-4.5.0.tgz#2882b7d01a6825dfdf909638f2de0256351def37"
integrity sha512-/PlTQCI96+fZMAOLMZK4CWG1ItCbfZ/0jx7UIJFChPNrx7tcEgerUgWbeieCM9MfHInUDyK8DWYZ+YrywDJuTg==
dependencies:
strnum "^1.0.5"

Expand Down

0 comments on commit c0601c3

Please sign in to comment.