joiful-ts / joiful

TypeScript Declarative Validation for Joi
239 stars 18 forks source link

Requesting support for validating inherited fields #222

Closed jpbaking closed 2 years ago

jpbaking commented 3 years ago

Given:

export class Foo {
  @joi.string().required().min(3).regex(/[a-zA-Z][a-zA-Z0-9-_]+[a-zA-Z0-9]/)
  foo: string;
}
export class Bar extends Foo {
  @joi.string().required().min(3).regex(/[a-zA-Z][a-zA-Z0-9-_]+[a-zA-Z0-9]/)
  bar: string;
}

When:

import * as jf from 'joiful';
const bar: Bar = new Bar();
// bar.foo // undefined
bar.bar = 'sample-val1d_value'
const { error } = jf.validate(bar);

Expect:

// jest-ish
expect(error?.details?.length).toStrictEqual(1) // should be because of bar.foo

Actual:

// jest-ish
expect(error?.details?.length).toStrictEqual(0) // bar.foo completely missed :'(
laurence-myers commented 3 years ago

Hmm, weird. We have tests for validating inherited properties, so it should be supported. We'll need to investigate.

https://github.com/joiful-ts/joiful/blob/master/test/unit/inheritance.test.ts

laurence-myers commented 3 years ago

I can't reproduce this issue. I added the test case to inheritance.test.ts, and it passes.

Is it possible that you have not imported reflect-metadata in your application's entry point, or you have imported it in multiple files? (Importing it mulitple times might create multiple metadata maps/caches)

    it('works for issue #222', () => {
        class Foo {
            @jf.string().required().min(3).regex(/[a-zA-Z][a-zA-Z0-9-_]+[a-zA-Z0-9]/)
            foo!: string;
        }
        class Bar extends Foo {
            @jf.string().required().min(3).regex(/[a-zA-Z][a-zA-Z0-9-_]+[a-zA-Z0-9]/)
            bar!: string;
        }

        const bar: Bar = new Bar();
        // bar.foo // undefined
        bar.bar = 'sample-val1d_value';
        const { error } = validator.validate(bar);
        expect(error?.details?.length).toStrictEqual(1); // should be because of bar.foo
    });

image

laurence-myers commented 2 years ago

Closing. If you still have this problem, please provide a minimal reproducible example.