Skip to content

Commit a7e8489

Browse files
committed
Add more data validation
1 parent 7f55561 commit a7e8489

File tree

3 files changed

+701
-4
lines changed

3 files changed

+701
-4
lines changed

data-processing.ts

+33-4
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,52 @@ function validateNumber(n: number) {
2222

2323
export function validatePayload(data: any) {
2424
// == Scale ==
25-
const scale = data.scale.scale;
25+
const scaleStore = data.scale;
26+
const scale = scaleStore.scale;
2627
if (scale.type !== 'ScaleWorkshopScale') {
2728
throw new Error('Invalid scale data');
2829
}
2930
for (const ratio of scale.intervalRatios) {
3031
validateNumber(ratio);
3132
}
33+
for (const color of scaleStore.colors) {
34+
validateString(color);
35+
}
36+
for (const label of scaleStore.labels) {
37+
validateString(label);
38+
}
3239
validateNumber(scale.baseFrequency);
3340
validateNumber(scale.baseMidiNote);
3441
validateString(scale.title, 4095);
3542
Interval.reviver('relativeIntervals', data.scale.relativeIntervals);
36-
validateString(data.scale.name, 4095);
37-
validateString(data.scale.sourceText, 65535);
43+
validateString(scaleStore.name, 4095);
44+
validateString(scaleStore.sourceText, 65535);
45+
validateString(scaleStore.error);
46+
validateString(scaleStore.warning);
47+
validateString(scaleStore.keyboardMode);
3848
// TODO: Rest
3949

4050
// == Audio ==
41-
validateString(data.audio.waveform);
51+
const audio = data.audio;
52+
validateNumber(audio.mainVolume);
53+
if (audio.mainVolume < 0 || audio.mainVolume > 1) {
54+
throw new Error('Invalid main volume');
55+
}
56+
validateNumber(audio.sustainLevel);
57+
if (audio.sustainLevel < 0 || audio.sustainLevel > 1) {
58+
throw new Error('Invalid sustain level');
59+
}
60+
validateNumber(audio.pingPongGain);
61+
if (audio.pingPongGain < 0 || audio.pingPongGain > 1) {
62+
throw new Error('Invalid ping pong gain');
63+
}
64+
validateNumber(audio.pingPongFeedback);
65+
const fb = Math.abs(audio.pingPongFeedback);
66+
if (fb > 1) {
67+
throw new Error('Invalid ping pong feedback');
68+
}
69+
validateString(audio.waveform);
70+
validateString(audio.aperiodicWaveform);
4271
// TODO: Rest
4372
return data;
4473
}

tests/data-processing.spec.ts

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import {expect, it, describe} from 'bun:test';
2+
import {cleanAndValidateEnvelope, validatePayload} from '../data-processing';
3+
4+
import TEST_SCALE from './test-scale.json';
5+
6+
describe('Payload validator', () => {
7+
it('validates the test scale', () => {
8+
const data = validatePayload(TEST_SCALE.payload);
9+
// TODO: Return boolean instead.
10+
expect(data).toBe(TEST_SCALE.payload);
11+
});
12+
});
13+
14+
describe('Envelope validator', () => {
15+
it('cleans and validates the test envelope', () => {
16+
expect(() => cleanAndValidateEnvelope(TEST_SCALE.envelope)).not.toThrow();
17+
});
18+
});

0 commit comments

Comments
 (0)