-
Notifications
You must be signed in to change notification settings - Fork 0
/
valid.html
71 lines (53 loc) · 2.17 KB
/
valid.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Odczyt pliku YAML</title>
<script src="https://cdn.jsdelivr.net/npm/ajv/dist/ajv.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/js-yaml@3/dist/js-yaml.min.js"></script>
<script src="js/validateYAML.js"></script>
</head>
<body>
<h1>Plik YAML</h1>
<input type="text" id="yaml-url" value="https://raw.githubusercontent.com/apiunit/www/master/project.yaml" />
<button onclick="loadYAML()">Odczytaj plik</button>
<button onclick="loadValidYAML()">Odczytaj i waliduj plik</button>
<pre id="yaml-content"></pre>
<script>
function loadYAML() {
var url = document.getElementById('yaml-url').value;
fetch(url)
.then(response => {
if (response.ok) return response.text();
throw new Error('Błąd sieci lub nie znaleziono pliku!');
})
.then(data => {
var yamlContent = jsyaml.load(data);
document.getElementById('yaml-content').textContent = JSON.stringify(yamlContent, null, 2);
})
.catch(error => {
document.getElementById('yaml-content').textContent = error.message;
});
}
function loadValidYAML() {
var url = document.getElementById('yaml-url').value;
var schemaUrl = 'schema.json';
Promise.all([fetch(url), fetch(schemaUrl)])
.then(responses => Promise.all(responses.map(res => res.text())))
.then(([yamlData, jsonSchema]) => {
const jsonData = jsyaml.load(yamlData);
const schema = JSON.parse(jsonSchema);
return validateYAML(jsonData, schema);
})
.then(isValid => {
document.getElementById('yaml-content').textContent = isValid ? 'YAML is valid!' : 'YAML is invalid!';
})
.catch(error => {
document.getElementById('yaml-content').textContent = error.message;
});
}
window.addEventListener('load', (event) => {
});
</script>
</body>
</html>