Code Monkey home page Code Monkey logo

Comments (14)

making avatar making commented on August 29, 2024

Unfortunately, YAVI does not support such cases.
As a workaround, you can define a validator for am.ik.yavi.fn.Pair

Example

Validator<Pair<Document, Param>> validator = ValidatorBuilder.<Pair<Document, Param>>of()
		.constraintOnObject(Pair::first, "id", c -> c.notNull().message("{0} not exists"))
		.constraintOnTarget(pair -> pair.first() == null || Objects.equals(pair.first().getOwnerId(), pair.second().getOwnerId()),
				"ownerId", "no_permission", "no permission")
		.build();


validator.validate(new Pair<>(documentDao.findById(param.getId()), param));

from yavi.

lonre avatar lonre commented on August 29, 2024

Unfortunately, YAVI does not support such cases.
As a workaround, you can define a validator for am.ik.yavi.fn.Pair

Example

Validator<Pair<Document, Param>> validator = ValidatorBuilder.<Pair<Document, Param>>of()
		.constraintOnObject(Pair::first, "id", c -> c.notNull().message("{0} not exists"))
		.constraintOnTarget(pair -> pair.first() == null || Objects.equals(pair.first().getOwnerId(), pair.second().getOwnerId()),
				"ownerId", "no_permission", "no permission")
		.build();


validator.validate(new Pair<>(documentDao.findById(param.getId()), param));

@making Thanks for your help.

but the full validator buider is

builder._string(Param::getFielda, "fielda", a -> a.notNull().notBlank());
builder._string(Param::getFieldb, "fieldb", a -> a.notNull().notBlank());
builder._long(Param::getId, "id", Constraint::notNull);
builder.constraintOnTarget(param -> {
  Document doc = documentDao.findById(param.getId());
  if (isNull(doc)) {
    return false;
  }
  return true;
}, "id", "not_exists", "id not exists");

builder.constraintOnTarget(param -> {
  Document doc = documentDao.findById(param.getId());
  if (!param.getOwnerId().equals(doc.getOwnerId())) {
    return false;
  }
  return true;
}, "ownerId", "no_permission", "no permission");

If there is some workaround please?

from yavi.

making avatar making commented on August 29, 2024

@lonre

Validator<Pair<Document, Param>> validator = new ValidatorBuilder<Pair<Document, Param>>("" /* message key separator */)
		.nest(Pair::second, "", b -> b
				.constraint(Param::getId, "id", c -> c.notNull())
				.constraint(Param::getFieldA, "fieldA", c -> c.notNull().notBlank())
				.constraint(Param::getFieldB, "fieldB", c -> c.notNull().notBlank()))
		.constraintOnObject(Pair::first, "id", c -> c.notNull().message("{0} not exists"))
		.constraintOnTarget(pair -> pair.first() == null || Objects.equals(pair.first().getOwnerId(), pair.second().getOwnerId()),
				"ownerId", "no_permission", "no permission")
		.build();

from yavi.

lonre avatar lonre commented on August 29, 2024

@lonre

Validator<Pair<Document, Param>> validator = new ValidatorBuilder<Pair<Document, Param>>("" /* message key separator */)
		.nest(Pair::second, "", b -> b
				.constraint(Param::getId, "id", c -> c.notNull())
				.constraint(Param::getFieldA, "fieldA", c -> c.notNull().notBlank())
				.constraint(Param::getFieldB, "fieldB", c -> c.notNull().notBlank()))
		.constraintOnObject(Pair::first, "id", c -> c.notNull().message("{0} not exists"))
		.constraintOnTarget(pair -> pair.first() == null || Objects.equals(pair.first().getOwnerId(), pair.second().getOwnerId()),
				"ownerId", "no_permission", "no permission")
		.build();

ok I see, Thanks for your help again @making

from yavi.

lonre avatar lonre commented on August 29, 2024

Hi @making

this is commons use cases in our projects, do you any idea or any plan to support this case

Thanks

from yavi.

making avatar making commented on August 29, 2024

It's hard to achieve it under the current design.

I would use caching like Spring Framework provides
https://spring.io/guides/gs/caching/

from yavi.

lonre avatar lonre commented on August 29, 2024

It's hard to achieve it under the current design.

I would use caching like Spring Framework provides
https://spring.io/guides/gs/caching/

Hi, Currently I am considering to fork yavi, and have a dirty hack. Extends ViolatedValue with specified violation details,

and hack the validate method, if ViolatedValue has details, use that, otherwise use original logic 😂

@making

from yavi.

making avatar making commented on August 29, 2024

Can you add test cases? I'm not sure what your commits want to achieve

from yavi.

lonre avatar lonre commented on August 29, 2024

Can you add test cases? I'm not sure what your commits want to achieve

Hi @making

test added

lonre@ccf8970

from yavi.

making avatar making commented on August 29, 2024

Sorry, but I can't accept your proposal.
Again, It's hard to achieve it under the current design.

It may require big changes to the code base

from yavi.

lonre avatar lonre commented on August 29, 2024

Sorry, but I can't accept your proposal.
Again, It's hard to achieve it under the current design.

It may require big changes to the code base

Hi @making

This make sense, and thanks for your time.

It is just dirty changes, and for my own usage.

from yavi.

atkawa7 avatar atkawa7 commented on August 29, 2024

@making Thanks for this library. I have been using yavi java-poet and jsonschema to create validators and this issue keeps on propping up. For example I have custom validators to validate phone numbers and jwt etc. However I have to parse the values twice one which I would discard and another which I would use because there is no caching on the return value.

from yavi.

making avatar making commented on August 29, 2024

@atkawa7 can you open a new issue? This is closed.
And please share a minimal reproducible working example to get the better context.

from yavi.

atkawa7 avatar atkawa7 commented on August 29, 2024

Thanks @making I have created a new ticket copying this one

from yavi.

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.