Giter Site home page Giter Site logo

Comments (5)

mabar avatar mabar commented on June 14, 2024

Whole purpose of validator is to accept only expected values. If you don't want to accept null value, just don't send the key at all. Or allow null:

$config = new Configuration([
    'test' => Expect::anyOf(Expect::string(), Expect::null())->default(null)
]);

$config->merge([
    'test' => null,
]);

$value = $config->get('test') ?? 'hey';

(example assumes you use thephpleague/config wrapper as it seems so)

from schema.

corex avatar corex commented on June 14, 2024

Hi,. Yes, forgot to menchion league/config :(

Apparently I am wrong in my understanding of default value.... I can set it as nullable, but then I have to take care of handling default value for both Except::class and in my own code. I am planning to write a wrapper of my own, so to maintain a default value two places is not good.

I am planning to have a config file with all properties in array with comments on i.e. https://github.com/laravel/laravel/blob/9.x/config/database.php

I am only using Laravel as an example. In this file you will see many properties with "null" specified. I need to have something similar and thought I found GOLD :D

from schema.

corex avatar corex commented on June 14, 2024

A variable containing null is an indication that the variable is not set. From my understanding the default() should apply here - hence my bug report. If it is a design choice, this bug report should be closed.

I can work around it by always setting Expect::.... to nullable() and handle the default values myself. That way I handle both the missing key in the array and if the key in the array is set to null - in my own code. In my opinion, this should not be necessary.

I might consider making this a feature request instead....

from schema.

dg avatar dg commented on June 14, 2024

A variable containing null is an indication th

I don't think so. Take a look at this configuration:

debug:
    logfile: null

Does this mean 1) disable logging or 2) use the default file? I think a lot of people would be confused if it behaved as 2). If I want to use the default behavior, I don't mention the key in config at all. When I mention it, it means I want to change something.

However, the behaviour you want to achieve is very easy to do:

function ignoreNulls($data)
{
    return array_filter($data, fn($val) => !is_null($val));
}

$schema = Expect::structure([
	'test' => Expect::type('string|null')->default('def'),
])->before('ignoreNulls');

from schema.

corex avatar corex commented on June 14, 2024

Based on your answer, I can conclude it is a design choice.

About the 1 & 2. I agree it can be confusing, but then I would argue that there is missing some comments in the yaml-file itself. And after reading both of your comments again, I think the best approach for me is simply to add comments in the file and comment out the "variable".

Thanks for the tip.

from schema.

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.