-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cap-gateway - change:schema validation middleware and trnaslations
- Loading branch information
1 parent
405ab11
commit 66d4b1f
Showing
9 changed files
with
538 additions
and
128 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { readFileSync } from 'fs'; | ||
import libxml from 'libxmljs'; | ||
import logger from '../logger.js'; | ||
|
||
import { HTTP_CODE } from '../constants.js'; | ||
|
||
export async function schemaValidator(req, res, next) { | ||
// middleware for validating received XML against provided schema XSD | ||
try { | ||
// Load XML and XSD files | ||
let xmlString = req.body.message ?? ''; | ||
const xsdString = readFileSync('cap.xsd', 'utf-8'); | ||
|
||
// Parse XML and XSD | ||
const xmlDoc = libxml.parseXml(xmlString); | ||
const xsdDoc = libxml.parseXml(xsdString); | ||
|
||
// Validate XML against XSD | ||
const isValid = xmlDoc.validate(xsdDoc); | ||
|
||
// Check validation result | ||
if (!isValid) { | ||
const validationErrors = xmlDoc.validationErrors; | ||
validationErrors.forEach(error => logger.error(error.message)); | ||
|
||
return res.status(HTTP_CODE.BadRequest).json({ | ||
error: 'XML does not conform to the Common Alert Protocol 1.2 XSD Schema', | ||
}); | ||
} | ||
} catch (error) { | ||
logger.error(error); | ||
} | ||
next(); | ||
} |
Oops, something went wrong.