-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
request: schema support #48
Comments
This is actually pretty trivially implemented using a custom defu already. Try something like this: import {createDefu} from "defu";
const testSchema = {
isPro: false,
darkMode: false,
pages: {
home: false,
settings: false,
},
}
const testInput = { isPro: 'bbb', d: 'c', pages: { home: true } }
const schemaDefu = createDefu((schema, key, _value) => {
// this func requires ESNext or ES2022 dialect
// otherwise use e.g. !Object.keys(schema).includes(key)
if (!Object.hasOwn(schema, key))
{
// skip merging in the value - it's not in the schema
return true;
}
// use defu's default merging behaviour otherwise
return false;
})
const result = schemaDefu(testInput, testSchema)
console.log(result); My output from the above is: {
isPro: 'bbb',
darkMode: false,
pages: { home: true, settings: false }
} |
ferferga
added a commit
to ferferga/defu
that referenced
this issue
Aug 27, 2023
Fixes unjs#48 Minor code improvement by using optional chaining as well when checking for merger function
8 tasks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
By default, giving a schema,
If the incoming new data is the same key, append the new data to the values of the schema keys.
delete keys that are not compatible with the schema.
Example
The text was updated successfully, but these errors were encountered: