Olian04 / Recordari

Recordari is a type and structure validation tool for configuration files
MIT License
4 stars 0 forks source link

Add optional properties for objects & default values for optional properties #15

Open Olian04 opened 6 years ago

Olian04 commented 6 years ago
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
});
Olian04 commented 5 years ago

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.