paazmaya / yaml-validator

Validate Yaml files and enforce a given structure
MIT License
56 stars 17 forks source link

Structure Validation does not work #70

Open alexej-strelzow opened 5 years ago

alexej-strelzow commented 5 years ago

Expected behaviour

options.structure should get considered by validator see bottom of this ticket, change e.g. description from string to number -> no effect

Actual behaviour

using options.structure with a structure object always works

report() always returns 1 and no warning gets printed out

Versions and environment

Thank you and have some :grapes:.

api.yml

globals:
  consumer:
    - event: "#/definitions/Register"
    - event: "#/definitions/Next"
    - event: "#/definitions/Previous"
    - event: "#/definitions/Destroy"
  producer:
    - event: "#/definitions/Initialize"

apps:
  - name: "login"
    consumer:
      - event: "#/definitions/Register"
      - event: "#/definitions/Next"
      - event: "#/definitions/Destroy"
    producer:

definitions:
  - event: "Register"
    name: "[Registration] register"
    description: "MF registers itself at the CL"

  - event: "Next"
    name: "[Routing] next"
    description: "MF tells the CL to route to the next MF"

  - event: "Previous"
    name: "[Routing] previous"
    description: "MF tells the CL to route to the previous MF"

  - event: "Initialize"
    name: "[Lifecycle] initialize"
    description: "CL may send the MF a configuration and/or a state (e.g. previous state - for-/backwards-navigation)"
    payload:
      - name: "config"
        type: "any"
      - name: "state"
        type: "any"

  - event: "Destroy"
    name: "[Lifecycle] destroy"
    description: "MF tells the CL that it has been destroyed and may pass the (latest) state"
    payload:
      - name: "state"
        type: "any"

api-schemal.validator.ts

import * as YamlValidator from 'yaml-validator';

export class ApiSchemaValidator {
  static structure = {
    globals: {
      consumer: [{ event: 'string' }],
      producer: [{ event: 'string' }],
    },
    apps: [
      {
        name: 'string',
        consumer: [{ event: 'string' }],
        producer: [{ event: 'string' }],
      },
    ],
    definitions: [
      {
        event: 'string',
        name: 'string',
        description: 'string',
        'payload?': [
          {
            name: 'string',
            type: 'string',
          },
        ],
      },
    ],
  };

  options: YamlValidator.IYamlValidatorOptions = {
    log: false,
    structure: ApiSchemaValidator.structure,
    onWarning: (error, filepath) => {
      console.warn(filepath + ' has error: ' + error);
    },
    writeJson: false,
  };

  validate(file: string): number {
    console.log(`Validating ${file}...`);

    // console.log(JSON.stringify(this.options));

    const validator = new YamlValidator(this.options);
    validator.validate([file]);
    return validator.report();
  }
}
alexej-strelzow commented 5 years ago

In addition, correct schema is wrongly validated as false.

E.g. adding events to producer below:

apps:
  - name: "login"
    consumer:
      - event: "#/definitions/Register"
      - event: "#/definitions/Next"
      - event: "#/definitions/Destroy"
    producer:
paazmaya commented 5 years ago

Thanks for the issue. Would you be willing to add these test cases to the unit tests via PR?

alexej-strelzow commented 5 years ago

Hi @paazmaya : I'm going to take another path and won't pursue yaml-validator anymore. I'm sorry.

Tzvetelin88 commented 3 years ago

Any success here, I have same issue with schema validation :( yaml file:

VRO.PLUGINS.USAGE.VCACCAFE: description: n/a weight: 0 impact: '' VRO.PLUGINS.USAGE.VCACCAFEINFRASTRUCTURE: weight: 0 impact: '' remedy: '[TBD]' CONTENT.COE: description: n/a weight: 0 impact: '' remedy: '[TBD]' CONTENT.SOVLABS: description: n/a weight: 0 impact: '' remedy: '[TBD]'

current configuration

const options = {
      log: 'false',
      structure: {
        'id?': {
          'description!': 'string',
          'weight!': 'number',
          'impact!': 'string',
          'remedy!': 'string'
        }
      },
      onWarning: undefined,
      writeJson: false
    };

    const validator = new YamlValidator(options);

    validator.validate([yamlFile]);

First record is missing "description", second one missing: "remedy".

If we have duplicate key it's working, but if something is missing from YAML file it's saying no errrors.

nipunadodan commented 2 weeks ago

Hi, any update on this? It looks like this still exists on the latest version.

paazmaya commented 1 week ago

Hi, thank you for the participation. Would you be willing to add these test cases to the unit tests via PR? I need to have test cases before I start finding out solution..