@baqhub/sdk
Version:
The official JavaScript SDK for the BAQ federated app platform.
163 lines (162 loc) • 5.25 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SchemaIO = void 0;
const tslib_1 = require("tslib");
const Either_js_1 = require("fp-ts/lib/Either.js");
const canonicalization_js_1 = require("./canonicalization.js");
const IO = tslib_1.__importStar(require("./io.js"));
const string_js_1 = require("./string.js");
const type_js_1 = require("./type.js");
function countUniqueItems(array) {
return new Set(array.map(canonicalization_js_1.Canonicalization.canonicalize)).size;
}
function schemaArray(itemsSchema, options = {}) {
const { minItems, maxItems, distinctItems } = options;
const baseType = IO.readonlyArray(itemsSchema);
function validateValue(value) {
if ((0, type_js_1.isDefined)(minItems) && value.length < minItems) {
return false;
}
if ((0, type_js_1.isDefined)(maxItems) && value.length > maxItems) {
return false;
}
if (distinctItems && countUniqueItems(value) !== value.length) {
return false;
}
return true;
}
function isSchemaArray(value) {
if (!baseType.is(value)) {
return false;
}
return validateValue(value);
}
return new IO.Type("SchemaArray", isSchemaArray, (value, context) => {
const result = baseType.validate(value, context);
if ((0, Either_js_1.isLeft)(result)) {
return IO.failure(value, context);
}
if (!validateValue(result.right)) {
return IO.failure(value, context, "Not a valid SchemaArray.");
}
return IO.success(result.right);
}, value => baseType.encode(value));
}
//
// Map.
//
function schemaMap(valuesSchema) {
return IO.record(IO.string, valuesSchema);
}
//
// Boolean.
//
function schemaBoolean() {
return IO.boolean;
}
function schemaString(options = {}) {
const { minLength, maxLength } = options;
const baseType = IO.string;
function validateValue(value) {
// Normalization.
if (value.normalize() !== value) {
return false;
}
if (!(0, type_js_1.isDefined)(minLength) && !(0, type_js_1.isDefined)(maxLength)) {
return true;
}
const length = string_js_1.Str.unicodeLength(value);
// MinLength.
if ((0, type_js_1.isDefined)(minLength) && length < minLength) {
return false;
}
// MaxLength.
if ((0, type_js_1.isDefined)(maxLength) && length > maxLength) {
return false;
}
return true;
}
function isSchemaString(value) {
if (!baseType.is(value)) {
return false;
}
return validateValue(value);
}
return new IO.Type("SchemaString", isSchemaString, (value, context) => {
const result = baseType.validate(value, context);
if ((0, Either_js_1.isLeft)(result)) {
return IO.failure(value, context);
}
if (!validateValue(result.right)) {
return IO.failure(value, context, "Not a valid SchemaString.");
}
return IO.success(result.right);
}, value => baseType.encode(value));
}
function schemaInt(options = {}) {
const { min, max } = options;
const baseType = IO.Int;
function validateValue(value) {
if ((0, type_js_1.isDefined)(min) && value < min) {
return false;
}
if ((0, type_js_1.isDefined)(max) && value > max) {
return false;
}
return true;
}
function isSchemaInt(value) {
if (!baseType.is(value)) {
return false;
}
return validateValue(value);
}
return new IO.Type("SchemaInt", isSchemaInt, (value, context) => {
const result = baseType.validate(value, context);
if ((0, Either_js_1.isLeft)(result)) {
return IO.failure(value, context);
}
if (!validateValue(result.right)) {
return IO.failure(value, context, "Not a valid SchemaInt.");
}
return IO.success(result.right);
}, value => value);
}
function schemaNumber(options = {}) {
const { min, max } = options;
const baseType = IO.number;
function validateValue(value) {
if ((0, type_js_1.isDefined)(min) && value < min) {
return false;
}
if ((0, type_js_1.isDefined)(max) && value > max) {
return false;
}
return true;
}
function isSchemaNumber(value) {
if (!baseType.is(value)) {
return false;
}
return validateValue(value);
}
return new IO.Type("SchemaNumber", isSchemaNumber, (value, context) => {
const result = baseType.validate(value, context);
if ((0, Either_js_1.isLeft)(result)) {
return IO.failure(value, context);
}
if (!validateValue(result.right)) {
return IO.failure(value, context, "Not a valid SchemaNumber.");
}
return IO.success(result.right);
}, value => value);
}
exports.SchemaIO = {
object: IO.dualObject,
array: schemaArray,
map: schemaMap,
boolean: schemaBoolean,
string: schemaString,
int: schemaInt,
number: schemaNumber,
};