Home > Enterprise >  Not allow duplicate tags in yml file
Not allow duplicate tags in yml file

Time:04-08

I am trying to validate my sample yml using yml schema file.

a.yml data file:

    test:
         version1
    test:
         version1

s.yml schema file:

     type: map
        mapping:
          test:
            type: str 
            required: yes
            unique: yes

In my perl code i am using YML inbuilt module and validate my data file with schema file as followed:

 eval { validate(YAML::LoadFile(s.yml), YAML::LoadFile(a.yml)) };

I was expecting to fail the validation because of having duplicate tags 'type:'. Is there a way to not allow duplicate tags in yml file while validating against schema schema.

I notice that loading is failing with warning:

Name "YAML::SortKeys" used only once: possible typo at test.plline 21.
YAML Warning: Duplicate map key found. Ignoring.

Code: YAML_LOAD_WARN_DUPLICATE_KEY Line: 1 Document: 1

Currently i am using 'use warnings FATAL => qw(all);', Still my script is passing. Not sure why it still pass with warnings. Can we make it Error?

CodePudding user response:

YAML::PP forbids duplicate keys by default (*).

use YAML::PP;
my $yaml = <<"EOM";
foo: a
foo: b
EOM
YAML::PP::Load($yaml);

__END__
Duplicate key 'foo' at /.../YAML/PP/Parser.pm line 61.

You are using YAML.pm, which is not recommened anymore, as it was written for YAML 1.0 and also has other problems.

(*) YAML::PP forbids duplicate keys since version 0.027. Before they were ignored.

  • Related