joiful-ts / joiful

TypeScript Declarative Validation for Joi
239 stars 18 forks source link

@string().allow not working #126

Closed fider-apptimia closed 4 years ago

fider-apptimia commented 4 years ago

@string().allow() pass unallowed strings.

Env:

Compiler options:

{
  "compilerOptions": {
    "esModuleInterop": true,
    "noImplicitAny": true,
    "module": "CommonJS",
    "moduleResolution": "node",
    "allowJs": true,
    "strict": true,
    "isolatedModules": true,
    "pretty": true,
    "sourceMap": true,
    "target": "es2018",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "rootDir": "./",
    "outDir": "./dist"
  },
  "exclude": ["dist", "node_modules"]
}

Reproduce:

import * as jf from 'joiful';

class C {
  @jf.string().allow('a', 'b').required()
  ab!: string;
}

const c = new C();
c.ab= 'x';

// expected error != null
console.dir(jf.validateAsClass(c, C));
// {
//  error: null,
//  value: C { ab: 'x' },
//  then: [Function: then],
//  catch: [Function: catch]
//}
laurence-myers commented 4 years ago

Looks like allow() is for allowing specific values in addition to the rest of the schema. So you could have a number() schema that also accepts the string "NaN".

https://github.com/hapijs/joi/blob/master/API.md#anyallowvalues

Note that this list of allowed values is in addition to any other permitted values. To create an exclusive list of values, see any.valid(value).

Try valid() instead, see if that works.

https://github.com/hapijs/joi/blob/master/API.md#anyvalidvalues---aliases-equal

fider-apptimia commented 4 years ago

Thanks