Comments (10)
@yaodingyd thanks, this has made me review my issue.
This is interesting. Adding default: {}
to foo
in my test above produces the desired results.
From what I can tell, draft 07 of the spec doesn't define the behaviour of a validator in this regard. Draft 08 adds some examples that happen to include handling of defaults, and seems to imply that the results I expected are correct (i.e. able to specify defaults on a sub-property without specifying that the property is default: {}
).
This sounds like an ajv
issue. Can anyone with a better understanding than myself weigh in?
from conf.
ajv
maintainers has decided that default
will not work for nested properties, if top level property has no default
value
This issue should be closed, as this library is using useDefaults
from ajv
for this feature.
from conf.
This is interesting. Adding
default: {}
tofoo
in my test above produces the desired results.
Isn't deault
property of foo
's bar
property scoped? That is, as far as I understand, Ajv will assign default property bar
to the foo object if the foo
object exists but bar
property does not.
That's why default
property on foo
level schema "works".
Here's an example and relevant section in Ajv docs.
from conf.
I'm not really sure what the problem is. We tell ajv
to use the defaults here:
Line 85 in 931ffce
from conf.
@issuehunt has funded $40.00 to this issue.
- Submit pull request via IssueHunt to receive this reward.
- Want to contribute? Chip in to this issue via IssueHunt.
- Checkout the IssueHunt Issue Explorer to see more funded issues.
- Need help from developers? Add your repository on IssueHunt to raise funds.
from conf.
@YodaDaCoda I cannot reproduce the case with basic type(string
). Can you give more details? (OS and node version)
About use object
type with default, I believe this is the correct behavior according to ajv: default keywords will be ingored if not in properties or items subschemas. For foo
, there is no default on its level, so it will be undefined. I use the following code and get the same result:
const Ajv = require('ajv');
const ajv = new Ajv({
allErrors: true,
format: 'full',
useDefaults: true,
errorDataPath: 'property'
});
const schema = {
type: "object",
properties: {
fizz: {
type: "object",
properties: {
bar: {
type: 'string',
default: 'baz',
},
},
},
},
};
const data = {};
var validate = ajv.compile(schema);
console.log(validate(data)); // true
console.log(data); // { }
The best we can do is document this in readme.
from conf.
@krnik true.
I think @YodaDaCoda is right in his expectation. When there is properties
it's obviously expected that foo
will/should be at least an empty {}
object no matter what.
@YodaDaCoda: Adding default: {} to foo in my test above produces the desired results.
At least the work around is easy, haha.
Yup, sounds like problem in ajv
and its logic.
from conf.
Yup, sounds like problem in ajv and its logic.
Can you open an issue on ajv
and comment the link here?
from conf.
I'm having similar problem, not sure if it's related. I've had:
const schema = {
reminders: {
type: 'array',
default: [{
uuid: uuidv4(),
type: 'once',
interval: 'every 10 seconds',
keep: true,
title: 'Welcome to LaterOn - The reminder app!',
body: 'Learn more at https://LaterOn.app.'
}]
}
}
Which worked fine, correct default value gets created.
Once I added another value to schema, it is not being added when I run the app again:
const schema = {
reminders: {
type: 'array',
default: [{
uuid: uuidv4(),
type: 'once',
interval: 'every 10 seconds',
keep: true,
title: 'Welcome to LaterOn - The reminder app!',
body: 'Learn more at https://LaterOn.app.'
}]
},
openAtLogin: {
type: 'boolean',
default: false
}
}
Am I doing something wrong or? I don't have object
as in previous examples, but simple boolean
.
Interestingly, if I remove my store file, on the app restart only the first array
default will get created, but not the second boolean
. If I remove array
altogether, boolean
will get created. If I change the schema so that boolean
is first, then both values get created.
So then I tried: put boolean
first and see what happens when array
already exists. Unfortunatelly that did not work, new default does not get created.
note: just noticed that this is for conf
. I am using electron-store
but I guess it makes no difference
from conf.
hey @sindresorhus I think this is indeed the bug in conf
(or electron-store
).
I've been playing with some workarounds here and elsewhere (where they check if store.has(key)
) just to find that the key
that I was missing (it did not get saved to the config file on app start) was existing and had correct value but was not saved to disk.
This seems to do the trick to save the file:
store.store = Object.assign({}, store.store)
I think that we need to save every time? not familiar with the code to say where the fix should go
try {
assert.deepEqual(fileStore, store);
} catch (_) {
this.store = store;
}
from conf.
Related Issues (20)
- Proxied object HOT 1
- Add a `mode` option HOT 1
- Debouncer causing the onDidChange callbacks to get delayed by 5 seconds when registered multiple times HOT 2
- Bad performance due to slow ajv.compile called. HOT 3
- Store.set is not type checked HOT 1
- onDidChange doesn't support dot-notation for nested properties
- Add option to use other encryption algorithm (i.e., AES-256-GCM) HOT 1
- Ajv is in strictmode HOT 8
- Support for ESM HOT 3
- Show example in readme of loading package.json when targeting Node.js 18
- `validate` after migration. HOT 10
- Is conf typescript-responsive or have type declarations? HOT 3
- "Cannot find module 'conf'" unless setting "moduleResolution": "node16" or "nodenext" HOT 1
- Documentation Request: File Creation & Location.
- Custom storage provider
- copyright-question HOT 2
- Can't import or require Conf from "conf' HOT 1
- Why we can no longer use require('conf') ? HOT 2
- Constructor throws exception if underlying Config is invalid HOT 3
- Switch from Buffer to Uint8Array HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from conf.