Coder Social home page Coder Social logo

Comments (3)

rubenvanassche avatar rubenvanassche commented on August 29, 2024

This has todo with #432, we're going to release v3.5.1 without this feature and iron out the kinks in v4.

from laravel-data.

bentleyo avatar bentleyo commented on August 29, 2024

@rubenvanassche thanks for the update 😄 I look forward to seeing the improvements you make in v4!

from laravel-data.

rubenvanassche avatar rubenvanassche commented on August 29, 2024

So I've tried this in data v4, which is kinda ready for release and uses the validated properties. This is the test:

it('handles issue B', function (){
    class ParentData extends Data
    {
        public function __construct(
            public bool $example_bool,
            public ChildData $child,
        )
        {
        }
    }

    class ChildData extends Data
    {
        public function __construct(
            public ?string $example_string = 'example',
            public ?int $example_int = 123,
        )
        {
        }
    }

    class DataPresentInsteadOfRequiredRuleInferrer implements RuleInferrer
    {
        public function handle(
            DataProperty $property,
            PropertyRules $rules,
            ValidationContext $context,
        ): PropertyRules
        {
            if (! $property->type->kind->isDataCollectable() && ! $property->type->kind->isDataObject()) {
                return $rules;
            }

            if (! $rules->hasType(Required::class)) {
                return $rules;
            }

            return $rules->removeType(Required::class)
                ->add(new Present());
        }
    }

    config()->set('data.rule_inferrers', [
        \Spatie\LaravelData\RuleInferrers\SometimesRuleInferrer::class,
        \Spatie\LaravelData\RuleInferrers\NullableRuleInferrer::class,
        \Spatie\LaravelData\RuleInferrers\RequiredRuleInferrer::class,
        \Spatie\LaravelData\RuleInferrers\BuiltInTypesRuleInferrer::class,
        \Spatie\LaravelData\RuleInferrers\AttributesRuleInferrer::class,
        DataPresentInsteadOfRequiredRuleInferrer::class,
    ]);

    request()->merge([
        'example_bool' => true,
        'child' => [],
    ]);

    $data = app(ParentData::class);

    dd($data);
});

Outputting:

Spatie\LaravelData\Tests\ParentData {#3414
  #_additional: []
  #_dataContext: null
  +example_bool: true
  +child: Spatie\LaravelData\Tests\ChildData {#3497
    #_additional: []
    #_dataContext: null
    +example_string: "example"
    +example_int: 123
  }
}

This looks allright to me? Could you test your use case with the beyond-data branch? That should almost be what data v4 will be.

from laravel-data.

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.