Skip to content

Commit

Permalink
Add more data validation
Browse files Browse the repository at this point in the history
  • Loading branch information
frostburn committed Jul 18, 2024
1 parent 7f55561 commit a7e8489
Show file tree
Hide file tree
Showing 3 changed files with 701 additions and 4 deletions.
37 changes: 33 additions & 4 deletions data-processing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,52 @@ function validateNumber(n: number) {

export function validatePayload(data: any) {
// == Scale ==
const scale = data.scale.scale;
const scaleStore = data.scale;
const scale = scaleStore.scale;
if (scale.type !== 'ScaleWorkshopScale') {
throw new Error('Invalid scale data');
}
for (const ratio of scale.intervalRatios) {
validateNumber(ratio);
}
for (const color of scaleStore.colors) {
validateString(color);
}
for (const label of scaleStore.labels) {
validateString(label);
}
validateNumber(scale.baseFrequency);
validateNumber(scale.baseMidiNote);
validateString(scale.title, 4095);
Interval.reviver('relativeIntervals', data.scale.relativeIntervals);
validateString(data.scale.name, 4095);
validateString(data.scale.sourceText, 65535);
validateString(scaleStore.name, 4095);
validateString(scaleStore.sourceText, 65535);
validateString(scaleStore.error);
validateString(scaleStore.warning);
validateString(scaleStore.keyboardMode);
// TODO: Rest

// == Audio ==
validateString(data.audio.waveform);
const audio = data.audio;
validateNumber(audio.mainVolume);
if (audio.mainVolume < 0 || audio.mainVolume > 1) {
throw new Error('Invalid main volume');
}
validateNumber(audio.sustainLevel);
if (audio.sustainLevel < 0 || audio.sustainLevel > 1) {
throw new Error('Invalid sustain level');
}
validateNumber(audio.pingPongGain);
if (audio.pingPongGain < 0 || audio.pingPongGain > 1) {
throw new Error('Invalid ping pong gain');
}
validateNumber(audio.pingPongFeedback);
const fb = Math.abs(audio.pingPongFeedback);
if (fb > 1) {
throw new Error('Invalid ping pong feedback');
}
validateString(audio.waveform);
validateString(audio.aperiodicWaveform);
// TODO: Rest
return data;
}
Expand Down
18 changes: 18 additions & 0 deletions tests/data-processing.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {expect, it, describe} from 'bun:test';
import {cleanAndValidateEnvelope, validatePayload} from '../data-processing';

import TEST_SCALE from './test-scale.json';

describe('Payload validator', () => {
it('validates the test scale', () => {
const data = validatePayload(TEST_SCALE.payload);
// TODO: Return boolean instead.
expect(data).toBe(TEST_SCALE.payload);
});
});

describe('Envelope validator', () => {
it('cleans and validates the test envelope', () => {
expect(() => cleanAndValidateEnvelope(TEST_SCALE.envelope)).not.toThrow();
});
});
Loading

0 comments on commit a7e8489

Please sign in to comment.