scotthovestadt / schema-object

Enforce schema on JavaScript objects, including type, transformation, and validation. Supports extends, sub-schemas, and arrays.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Support 'keysIgnoreAccents' feature to ignore accents in keys?

anthony-o opened this issue · comments

I have a use case where people could input accent or not in keys and I want a specific version of my keys.

You could use theKey.normalize('NFD') to do such a job, see this SO answer to remove accents in javascript strings.

Feel free to submit a PR, as long as it's behind a flag and includes unit tests I'll accept it.

I would call it keysAutoNormalized.

Here it is, with unit tests, what do you think about this PR? (I added a TODO inside in order to later on think of a way to boost key searches with some cache...)

Looks pretty good.

  1. Remove the TODO comments. If there's an optimization there (not sure there is) feel free to create an issue to track it. Any performance improvements should have benchmarks to prove it.

  2. Update the unit tests to ensure both read and write access work correctly. You currently have:

o.schéma = 'a string';
o.schema.should.equal('a string');

We need to check all cases. So, for example:

o.schéma = 'a string';
o.schéma.should.equal('a string');
o.schema.should.equal('a string');

o.schema = 'new string';
o.schéma.should.equal('new string');
o.schema.should.equal('new string');

I don't understand, I think, given the following code:

            var SO = new SchemaObject({
                schema: String
            }, {
                keysAutoNormalized: true
            });

            var o = new SO();
            o.schéma = 'a string';
            o.schema.should.equal('a string');

I would expect:

            should.equal(o.schéma, null);

The same happens with keysIgnoreCase:

            var SO = new SchemaObject({
                profileURL: String
            }, {
                keysIgnoreCase: true
            });

            var o = new SO();
            o.profileUrl = 'a string';
            o.profileURL.should.equal('a string');

            should.equal(o.profileUrl, null); // which is currently the case by the way