const { Record, R } = require('record.js');
const ROptionals = Record('Optionals', {
foo: R.Number, // Required
'bar?': R.Number, // Optional, but need to be a number if pressent
'bar!': 2, // Default value for 'bar' if none was passed in.
'?': R.Number // '?' applies to all unknown keys, if its missing then unknown keys are prohibited
});
const okRecord = ROptionals({
foo: 1,
// bar = 2
biz: 3
});
const failRecord = ROptionals({
foo: '1', // Error: Settings.foo => '1' is not a number
bar: '2', // Error: Settings.bar => '2' is not a number
biz: '3' // Error: Settings.biz => '3' is not a number
});
Maybe the default should be optional, and required should be opt in?
In that case then default values needs to be tightly integrated into the core api... somehow.