1
0
Fork 0
mirror of synced 2024-09-09 22:16:26 +12:00
budibase/packages/core/src/types/bool.js
2019-09-28 05:28:11 +01:00

40 lines
1,012 B
JavaScript

import { constant, isBoolean, isNull } from 'lodash/fp';
import {
typeFunctions,
makerule, parsedFailed, parsedSuccess,
getDefaultExport,
} from './typeHelpers';
import {
switchCase, defaultCase, isOneOf, toBoolOrNull,
} from '../common';
const boolFunctions = typeFunctions({
default: constant(null),
});
const boolTryParse = switchCase(
[isBoolean, parsedSuccess],
[isNull, parsedSuccess],
[isOneOf('true', '1', 'yes', 'on'), () => parsedSuccess(true)],
[isOneOf('false', '0', 'no', 'off'), () => parsedSuccess(false)],
[defaultCase, parsedFailed],
);
const options = {
allowNulls: {
defaultValue: true,
isValid: isBoolean,
requirementDescription: 'must be a true or false',
parse: toBoolOrNull,
},
};
const typeConstraints = [
makerule(async (val, opts) => opts.allowNulls === true || val !== null,
() => 'field cannot be null'),
];
export default getDefaultExport(
'bool', boolTryParse, boolFunctions,
options, typeConstraints, true, JSON.stringify,
);