Giter Site home page Giter Site logo

Comments (4)

imbrn avatar imbrn commented on June 15, 2024

Hey @celluj34 ,

Can you share an example of your scenario, please?

For custom rules, you can take a look at the documentation section: https://imbrn.dev/v8n/Extending.html

from v8n.

celluj34 avatar celluj34 commented on June 15, 2024

Sure thing, something like the below:

class User {
  id: string;
  name: string;
  canLoginAfter: Date;
  canLoginUntil: Date;
}

const user: User = {
  id: "<some guid value>",
  name: "celluj34",
  canLoginAfter: new Date("2022-01-01"),
  canLoginUntil: new Date("2021-01-01") //see these dates are in backwards order
}

const result = v8n()
  .schema({
      canLoginUntil: v8n().greaterThan( /* ??? */ ) // should compare to user.canLoginAfter
  })
  .test(user) //expected: false

from v8n.

imbrn avatar imbrn commented on June 15, 2024

@celluj34 , thank you for sharing your code. :)

tldr; Sorry, but today v8n doesn't have validation based on any kind of context data (like an object being validated by schema rule). But having that feature seems really exciting to me.

We could design something like this:

const myCustomRule = (ruleConfig) = (value, context) => context.prop == ruleConfig.expectedProp && value;

But to achieve that, we'd have to change how validation rules are executed today. But that's really exciting.

I'll make sure to keep this thread linked with a future proposal of this feature so that you can follow it along.

from v8n.

celluj34 avatar celluj34 commented on June 15, 2024

Okay, thanks for the info! Yeah I was hoping to do something similar to FluentValidation from C#, I suppose I could create a new v8n().schema per object under test, but that seems a bit wasteful if you're creating a lot of them:

const genericUserValidator = (user) => v8n()
  .schema({
    canLoginUntil: v8n().greaterThan(user.canLoginAfter.valueOf())
  });

const userValidator = genericUserValidator(user);

const result = userValidator.test(user) //expected: false

from v8n.

Related Issues (20)

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.