mas-piano-validator

This repository contains the core library to validate the piano files used in the Monika After Story DDLC Mod.

Usage no npm install needed!

<script type="module">
  import masPianoValidator from 'https://cdn.skypack.dev/mas-piano-validator';
</script>

README

MAS Piano Songs Validator

Build Status XO code style Maintainability

This repository contains the core library to validate the piano files used in the DDLC Mod "Monika After Story".

The Credits file clearly states who is the author of every song example you can find in this repository.

You can find the corresponding CLI application here.

Introduction

In MAS you can play the piano to Monika and, depending on how the songs are described, she reacts to the song played.

The mod recognizes all piano songs by parsing JSON formatted files.

This library allows you to validate the structure of the songs you are working on.

This project does not aim to check for the actual correctness of the song you are authoring.

Installation

Note: to use this library, you have to have installed Node.js and a console you can run commands into. The minimum required version of Node.js is: 8 - codename "Carbon".

In your console, run the following command:

$ npm install mas-piano-validator

You can also use yarn (like we do in this project):

$ yarn add mas-piano-validator

Usage

The library exports a single function you can use to validate your object.

It is critical that you pass in an already parsed JSON object. This may change in the future, but for the moment the library expects you to parse the JSON.

Simple example

const validate = require('mas-piano-validator');

const validMASPiano = {
    "name": "Song name",
    "verse_list": [0],
    "pnm_list": [
        {
            "text": "One",
            "style": "monika_credits_text",
            "notes": [
                "D5",
                "C5SH",
                "B4",
                "F4SH"
            ]
        },
        {
            "text": "Two",
            "style": "monika_credits_text",
            "notes": [
                "D5",
                "A4",
                "D5",
                "A4"
            ]
        }
    ]
};

const result = validate(validMASPiano);

console.log(result.ok); // true
console.log(result.errors); // []

Example with multiple input (all API)

const validate = require('mas-piano-validator');

const validMASPiano = {
    "name": "Song name",
    "verse_list": [0],
    "pnm_list": [
        {
            "text": "One",
            "style": "monika_credits_text",
            "notes": [
                "D5",
                "C5SH",
                "B4",
                "F4SH"
            ]
        },
        {
            "text": "Two",
            "style": "monika_credits_text",
            "notes": [
                "D5",
                "A4",
                "D5",
                "A4"
            ]
        }
    ]
};

const container = new validate.ValidationInputContainer();
container.add(validMASPiano, 'test1').add(validMASPiano, 'test2');

const results = validate.all(container);
console.log(results.ok); // true
console.log(results.summary); // All files are valid Monika After Story piano songs.

Parse existing file

const fs = require('fs');
const {promisify} = require('util');

const validate = require('mas-piano-validator'); 

// We are going to use the promisified version of the fs.readFile function 
const readFile = promisify(fs.readFile);

const someFilePath = '...';

async function main() {
    const fileContent = await readFile(someFilePath, {encoding: 'utf8'});
    // Optional object, useful when you want to keep track of validation results
    const meta = {
        src: someFilePath
    };
    // Here you should handle the error in case the file is not a valid JSON file
    try {
        console.log(validate(fileContent, meta).ok); // true or false depending on the file...
    }
}

For other usage examples you can take a look at:

Accessing the schema file

If you want you can access the JSON Schema document used in this library for JSON validation.

The simplest way of accessing it is using require:

const pianoSchema = require('mas-piano-validator/schema/piano.schema.json')

Related