Skip to content

Datatypes

William Giles edited this page Dec 4, 2018 · 3 revisions

Data types are the primitive types native to Javascript (with the exception of the computed data type). These are used to define a schema. They can be extended and overwritten to create custom types such as email, url or others.

Extending

Nativemodels provides createType to allow you to extend the base datatype and build custom types for your own use.

const { createType } = require('nativemodels');

const guid = () =>
	createType({
		name: 'guid',
		parse: (key, value) => value.toUpperCase(),
		validCheck: (key, value) => {
			if (value === '00000000-0000-0000-0000-000000000000') {
				return true;
			} else if (/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/iu.test(value)) {
				return true;
			}

			throw new Error(`Property ${key} is not a GUID`);
		},
	});

module.exports = guid;

More information on extending can be found here: Extending Types

Clone this wiki locally