Giter Site home page Giter Site logo

amauryd / fastest-validator-decorators Goto Github PK

View Code? Open in Web Editor NEW
17.0 4.0 6.0 268 KB

Typescript decorators for the fastest-validator library

License: MIT License

TypeScript 100.00%
decorators fastest-validator schema typescript validate validation validator

fastest-validator-decorators's People

Contributors

alexhutsau avatar amauryd avatar dependabot[bot] avatar masonlouchart avatar tobydeh avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

fastest-validator-decorators's Issues

Extending SchemaBase is not producing object with values

fastest-validator: 1.12.0

When I follow the example to extend SchemaBase to use the constructor get a new object, the property values are always undefined, (except any properties that are on the original object but not in the schema - they remain untouched).

i.e.

@Schema()
class Entity extends SchemaBase { // BTW I think the docs have a typo, const should be class
  @Email()
  email: string;
}

const entity = new Entity({ email: '[email protected]' });
console.log(entity); // Entity { email: undefined }

I put some console.logs into the src at https://github.com/AmauryD/fastest-validator-decorators/blob/master/src/index.ts#L148 and can confirm at this point the object has all the values. But somewhere they are getting lost.

Migration to 2

When following the migration guide and using the same helper function, get the error:

Obj is missing complied validation method

PR

Hi, thanks for you great job.

i wont to make PR, but i can't push new brunch .
can you add me write access to do it?

"Nested" decorator on react enviroment (or front-end, webpack?)

Hello.

First of all, thank you for making a good module.

My React project is using this module well.

There is a bug when using the "Nested" decorator, as the title suggests.

I don't know exactly what kind of environment it is.

Below is a reproduced code and box link.

Please check it and reflect it.

https://codesandbox.io/s/fastest-validator-decorators-test-bhm7tf

  • These are screenshots of error parts and resolution parts for reference.

I added this line and solved it.
image

Error when this line is missing.
image

Error log in the actual create-react-app environment.
image

Async support

Hello,

I made a pull request #25 to support async validation introduced in fastest-validator library.
In order not to break semver, i added a custom decorator @AsyncSchema that behaves the same than @Schema.

Also, should we use the library "fastest-validator" as peerDependency instead of dependency ?

Extending a schema class mixes up properties

Hello !

When extending an annotated class, the resulting schemas contain properties of both classes which get mixed up. An example:

import { getSchema, Schema, String } from 'fastest-validator-decorators';

@Schema()
export class Parent {
    @String()
    a!: string;
}

@Schema()
export class Child extends Parent {
    @String()
    b!: string;
}

console.log(getSchema(Parent));
console.log(getSchema(Child));

Produces:

{
  a: { empty: false, type: 'string' },
  '$$strict': false,
  b: { empty: false, type: 'string' }
}
{
  a: { empty: false, type: 'string' },
  '$$strict': false,
  b: { empty: false, type: 'string' }
}

whereas one would expect

{
  a: { empty: false, type: 'string' },
  '$$strict': false,
}
{
  a: { empty: false, type: 'string' },
  '$$strict': false,
  b: { empty: false, type: 'string' }
}

Versions used:

  • fastest-validator: ^1.11.0
  • fastest-validator-decorators: ^1.2.1

Thank you for your time !

Custom validation example

Hi there,

I struggle to implement custom validation on a property of type array. I want to check the items of the array (if there is any) are all instances of a given class. I tried passing custom validation function based on the fastest-validator documentation. The following snippet gives you the idea of what I need.

Capture d’écran 2021-01-25 à 11 37 20

Did someone achieve it?
Thanks for your help.

Error with Nested decorator

I have used Nested as below:
`@Schema(true)
class Entity {
@Email()
email: string
}

@Schema(true)
export class CreateBannerInput {
@nested()
prop: Entity
}`

But I received an error, I don't know why, please help me !!!

✖ Cannot read property 'prototype' of undefined
✖ TypeError: Cannot read property 'prototype' of undefined
at getSchema (/Users/Projects/xxx/node_modules/fastest-validator-decorators/dist/index.js:39:59)

Default values

First of all, thank you for this project, it is very useful. I like to put validation directly into my schemas. 🤟

Unfortunately I was not able to make it work with default values. Following your example, this class bellow does not compile with strict mode enabled.

@Schema()
class Entity extends SchemaBase {
    @String({ min: 4, max: 8, trim: true })
    prop1: string; // <= ERROR: Property 'prop1' has no initializer and is not definitely assigned in the constructor
}

When I set a default value directly in the property definition (what I would like to do actually) all passed values at the instantiation time are ignored.

@Schema()
class Entity extends SchemaBase {
    @String({ min: 4, max: 8, trim: true })
    prop1 = "0000";
}

const entity = new Entity({ prop1: "1234" });
console.log(entity); // print -> {prop1: "0000"}

In the case I disable the strict mode (I would prefer to keep it enabled) and try to define a default value inside the decorator (expecting to use the fastest-validator feature), it does not work neither. This time it is the default value which is ignored.

@Schema()
class Entity extends SchemaBase {
    @String({ min: 4, max: 8, trim: true, default: "0000" })
    prop1: string;
}

const entity1 = new Entity({ prop1: "1234" });
console.log(entity1); // print -> {prop1: "1234"}
const entity2 = new Entity({});
console.log(entity2); // print -> {}

Do I do something wrong? Is there a solution?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.