Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

schemaglobin

Package Overview
Dependencies
Maintainers
1
Versions
64
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

schemaglobin - npm Package Compare versions

Comparing version 5.4.3 to 5.5.0

23

dist/helpers/message.d.ts

@@ -9,2 +9,3 @@ /**

export declare class Message {
readonly status?: string;
readonly message: string;

@@ -27,5 +28,25 @@ readonly details?: Record<string, Message>;

/** Convert a Message to a JSON format. */
toJSON(): string | Record<string, unknown>;
toJSON(): Record<string, unknown>;
/** Create a Message from its corresponding JSON format. */
static fromJSON(json: unknown): Message;
/** Create a new Message of a registered type. */
static create(status: string, message: string, details?: Record<string, Message>): Message;
/** Register a new Message class. */
static register(name: string, constructor: typeof Message): void;
}
/** Specific type of `Message` to indicate success. */
export declare class Success extends Message {
readonly status: string;
}
/** Specific type of `Message` to indicate warning. */
export declare class Warning extends Message {
readonly status: string;
}
/** Specific type of `Message` returned from `validate()` when a value is invalid. */
export declare class Invalid extends Message {
readonly status: string;
}
/** Specific type of `Invalid` returned when a value is required. */
export declare class Required extends Invalid {
readonly status: string;
}
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Message = void 0;
exports.Required = exports.Invalid = exports.Warning = exports.Success = exports.Message = void 0;
const object_1 = require("./object");

@@ -40,21 +40,74 @@ const entryToString = ([key, message]) => `- ${key}: ${message.toString().replace(/\n/g, "\n ")}`;

toJSON() {
if (!this.details)
return this.message;
return {
message: this.message,
details: object_1.mapObject(this.details, messageToJSON),
};
const json = { message: this.message };
if (this.status)
json.status = this.status;
if (this.details)
json.details = object_1.mapObject(this.details, messageToJSON);
return json;
}
/** Create a Message from its corresponding JSON format. */
static fromJSON(json) {
// String format is simple.
if (typeof json === "string")
return new Message(json);
// Object format is slightly more compled.
if (object_1.isObject(json) && typeof json.message === "string")
return new Message(json.message, object_1.isObject(json.details) ? object_1.mapObject(json.details, Message.fromJSON) : undefined);
if (object_1.isObject(json) && typeof json.message === "string") {
const { status, message, details } = json;
const parsedDetails = object_1.isObject(details) ? object_1.mapObject(details, Message.fromJSON) : undefined;
if (typeof status !== "string")
return new Message(message, parsedDetails);
if (registry[status])
return new registry[status](message, parsedDetails);
throw new Error(`Invalid status "${status}"`);
}
// Anything else is impossible.
throw new Error("Incorrect JSON format");
}
/** Create a new Message of a registered type. */
static create(status, message, details) {
if (registry[status])
return new registry[status](message, details);
throw new Error(`Invalid status "${status}"`);
}
/** Register a new Message class. */
static register(name, constructor) {
registry[name] = constructor;
}
}
exports.Message = Message;
/** Specific type of `Message` to indicate success. */
class Success extends Message {
constructor() {
super(...arguments);
this.status = "success";
}
}
exports.Success = Success;
/** Specific type of `Message` to indicate warning. */
class Warning extends Message {
constructor() {
super(...arguments);
this.status = "warning";
}
}
exports.Warning = Warning;
/** Specific type of `Message` returned from `validate()` when a value is invalid. */
class Invalid extends Message {
constructor() {
super(...arguments);
this.status = "invalid";
}
}
exports.Invalid = Invalid;
/** Specific type of `Invalid` returned when a value is required. */
class Required extends Invalid {
constructor() {
super(...arguments);
this.status = "required";
}
}
exports.Required = Required;
// Store a list of message classes linked to strings.
const registry = {
success: Success,
warning: Warning,
invalid: Invalid,
required: Required,
};

1

dist/index.d.ts

@@ -20,3 +20,2 @@ export * from "./types";

export * from "./helpers/distance";
export * from "./helpers/invalid";
export * from "./helpers/message";

@@ -23,0 +22,0 @@ export * from "./helpers/null";

@@ -36,3 +36,2 @@ "use strict";

__exportStar(require("./helpers/distance"), exports);
__exportStar(require("./helpers/invalid"), exports);
__exportStar(require("./helpers/message"), exports);

@@ -39,0 +38,0 @@ __exportStar(require("./helpers/null"), exports);

@@ -1,2 +0,2 @@

import type { Invalid } from "./helpers/invalid";
import type { Invalid } from "./helpers/message";
/**

@@ -3,0 +3,0 @@ * SchemaOptions enforces types on the options bag that is passed into SchemaClass to create Schema.

import { Schema, SchemaOptions, SchemaType } from "../Schema";
import { Invalid } from "../helpers/invalid";
import { Invalid } from "../helpers/message";
/** Coerce an unknown value to an array (if possible). */

@@ -4,0 +4,0 @@ export declare function coerceArray(value: unknown): unknown[] | Invalid;

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.array = exports.ArraySchema = exports.coerceArray = void 0;
const invalid_1 = require("../helpers/invalid");
const message_1 = require("../helpers/message");
/** Coerce an unknown value to an array (if possible). */

@@ -11,3 +11,3 @@ function coerceArray(value) {

return value;
return new invalid_1.Invalid("Must be array");
return new message_1.Invalid("Must be array");
}

@@ -59,3 +59,3 @@ exports.coerceArray = coerceArray;

const unsafeArray = coerceArray(unsafeValue);
if (unsafeArray instanceof invalid_1.Invalid)
if (unsafeArray instanceof message_1.Invalid)
return unsafeArray;

@@ -66,3 +66,3 @@ // Has contents?

if (this.required)
return new invalid_1.Required("Required");
return new message_1.Required("Required");
// Return empty array.

@@ -74,6 +74,6 @@ // We know this assertion is okay because we know the array is empty.

if (typeof this.min === "number" && unsafeArray.length < this.min)
return new invalid_1.Invalid(`Minimum ${this.min} items`);
return new message_1.Invalid(`Minimum ${this.min} items`);
// Array longer than max length returns Invalid.
if (typeof this.max === "number" && unsafeArray.length > this.max)
return new invalid_1.Invalid(`Maximum ${this.max} items`);
return new message_1.Invalid(`Maximum ${this.max} items`);
// Check each item against `this.items`

@@ -88,3 +88,3 @@ let changed = false;

const value = items.validate(current);
if (value instanceof invalid_1.Invalid) {
if (value instanceof message_1.Invalid) {
invalid = true;

@@ -101,3 +101,3 @@ invalids[i.toString()] = value;

if (invalid)
return new invalid_1.Invalid("Invalid items", invalids);
return new message_1.Invalid("Invalid items", invalids);
// Return the new array if it changed.

@@ -104,0 +104,0 @@ // We know this assertion is okay because if it wasn't, we would've returned Invalid.

import type { Schema, SchemaOptions } from "../Schema";
import type { FalseIfOptional } from "../types";
import { Invalid } from "../helpers/invalid";
import { Invalid } from "../helpers/message";
/** Coerce an unknown value to a boolean. */

@@ -5,0 +5,0 @@ export declare function coerceBoolean(value: unknown): boolean;

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.boolean = exports.BooleanSchema = exports.coerceBoolean = void 0;
const invalid_1 = require("../helpers/invalid");
const message_1 = require("../helpers/message");
/** Coerce an unknown value to a boolean. */

@@ -26,3 +26,3 @@ function coerceBoolean(value) {

if (this.required && !value)
return new invalid_1.Required("Required");
return new message_1.Required("Required");
// Return boolean.

@@ -29,0 +29,0 @@ return value;

import type { NullIfOptional } from "../types";
import type { Schema, SchemaOptions } from "../Schema";
import { Invalid } from "../helpers/invalid";
import { Invalid } from "../helpers/message";
/** Coerce an unknown value to a Color (if possible). */

@@ -5,0 +5,0 @@ export declare function coerceColor(value: unknown): string | null | Invalid;

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.color = exports.ColorSchema = exports.coerceColor = void 0;
const invalid_1 = require("../helpers/invalid");
const message_1 = require("../helpers/message");
const R_COLOR = /^#[0-9A-F]{6}$/;

@@ -20,3 +20,3 @@ /**

return cleanColor(value) || null;
return new invalid_1.Invalid("Must be string or null");
return new message_1.Invalid("Must be string or null");
}

@@ -45,3 +45,3 @@ exports.coerceColor = coerceColor;

const value = coerceColor(unsafeValue);
if (value instanceof invalid_1.Invalid)
if (value instanceof message_1.Invalid)
return value;

@@ -52,3 +52,3 @@ // Null means 'no color'

if (this.required)
return new invalid_1.Required("Required");
return new message_1.Required("Required");
// Return null.

@@ -60,3 +60,3 @@ // We know this type assertion is sound because `null` can never be returned if `this.required == true`.

if (!R_COLOR.test(value))
return new invalid_1.Invalid("Invalid color (must be a hexadecimal color)");
return new message_1.Invalid("Invalid color (must be a hexadecimal color)");
// Return the normalised Color.

@@ -63,0 +63,0 @@ return value;

import type { Schema, SchemaOptions } from "../Schema";
import type { NullIfOptional } from "../types";
import { Invalid } from "../helpers/invalid";
import { Invalid } from "../helpers/message";
export declare type DateSchemaType<R extends boolean = false> = string | NullIfOptional<R>;

@@ -5,0 +5,0 @@ /** Convert an unknown value to a date string (or return Invalid) */

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.date = exports.DateSchema = exports.coerceDate = void 0;
const invalid_1 = require("../helpers/invalid");
const message_1 = require("../helpers/message");
const date_1 = require("../helpers/date");

@@ -20,3 +20,3 @@ /** Convert an unknown value to a date string (or return Invalid) */

return new Date(value); // Strings are converted to dates (except empty string which should be "").
return new invalid_1.Invalid("Must be date, number, string, or null");
return new message_1.Invalid("Must be date, number, string, or null");
}

@@ -42,3 +42,3 @@ exports.coerceDate = coerceDate;

const value = coerceDate(unsafeValue);
if (value instanceof invalid_1.Invalid)
if (value instanceof message_1.Invalid)
return value;

@@ -49,3 +49,3 @@ // Explicit null means 'no date'.

if (this.required)
return new invalid_1.Required("Required");
return new message_1.Required("Required");
// Return null.

@@ -57,10 +57,10 @@ // We know this type assertion is sound because `null` can never be returned if `this.required == true`.

if (Number.isNaN(value.getTime()))
return new invalid_1.Invalid("Invalid date");
return new message_1.Invalid("Invalid date");
// Enforce min/max.
const minDate = coerceDate(this.min);
if (minDate instanceof Date && value.getTime() < minDate.getTime())
return new invalid_1.Invalid(`Minimum ${minDate.toLocaleDateString()}`);
return new message_1.Invalid(`Minimum ${minDate.toLocaleDateString()}`);
const maxDate = coerceDate(this.max);
if (maxDate instanceof Date && value.getTime() > maxDate.getTime())
return new invalid_1.Invalid(`Maximum ${maxDate.toLocaleDateString()}`);
return new message_1.Invalid(`Maximum ${maxDate.toLocaleDateString()}`);
// Return the valid date string.

@@ -67,0 +67,0 @@ return date_1.getYmd(value);

@@ -1,2 +0,2 @@

import { Invalid } from "../helpers/invalid";
import { Invalid } from "../helpers/message";
import { SchemaOptions, Schema } from "../Schema";

@@ -3,0 +3,0 @@ import { NullIfOptional } from "../types";

@@ -5,3 +5,3 @@ "use strict";

const NumberSchema_1 = require("./NumberSchema");
const invalid_1 = require("../helpers/invalid");
const message_1 = require("../helpers/message");
const distance_1 = require("../helpers/distance");

@@ -30,3 +30,3 @@ const number_1 = require("../helpers/number");

let value = NumberSchema_1.coerceNumber(unsafeValue);
if (value instanceof invalid_1.Invalid)
if (value instanceof message_1.Invalid)
return value;

@@ -37,3 +37,3 @@ // Null means 'no number'

if (this.required)
return new invalid_1.Required("Required");
return new message_1.Required("Required");
// Return null.

@@ -45,3 +45,3 @@ // We know this type assertion is sound because `null` can never be returned if `this.required == true`.

if (!Number.isFinite(value))
return new invalid_1.Invalid("Must be finite number");
return new message_1.Invalid("Must be finite number");
// Convert units, e.g. `10km` into the target dimension.

@@ -51,3 +51,3 @@ if (typeof value === "number" && typeof unsafeValue === "string") {

if (!detectedUnit)
return new invalid_1.Invalid("Invalid distance");
return new message_1.Invalid("Invalid distance");
if (detectedUnit !== this.unit)

@@ -58,5 +58,5 @@ value = distance_1.convertDistance(value, detectedUnit, this.unit);

if (typeof this.max === "number" && value > this.max)
return new invalid_1.Invalid(`Maximum ${this.max}`);
return new message_1.Invalid(`Maximum ${this.max}`);
if (typeof this.min === "number" && value < this.min)
return new invalid_1.Invalid(`Minimum ${this.min}`);
return new message_1.Invalid(`Minimum ${this.min}`);
// Round to step.

@@ -63,0 +63,0 @@ if (typeof this.step === "number")

import type { Schema, SchemaOptions } from "../Schema";
import type { NullIfOptional } from "../types";
import { Invalid } from "../helpers/invalid";
import { Invalid } from "../helpers/message";
/** Coerce an unknown value to an email (if possible). */

@@ -5,0 +5,0 @@ export declare function coerceEmail(value: unknown): string | null | Invalid;

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.email = exports.EmailSchema = exports.coerceEmail = void 0;
const invalid_1 = require("../helpers/invalid");
const message_1 = require("../helpers/message");
/** Coerce an unknown value to an email (if possible). */

@@ -11,3 +11,3 @@ function coerceEmail(value) {

return value.trim().toLowerCase() || null;
return new invalid_1.Invalid("Must be string or null");
return new message_1.Invalid("Must be string or null");
}

@@ -48,3 +48,3 @@ exports.coerceEmail = coerceEmail;

const value = coerceEmail(unsafeValue);
if (value instanceof invalid_1.Invalid)
if (value instanceof message_1.Invalid)
return value;

@@ -55,3 +55,3 @@ // Null means 'no email'

if (this.required)
return new invalid_1.Required("Required");
return new message_1.Required("Required");
// Return null.

@@ -63,6 +63,6 @@ // We know this type assertion is sound because `null` can never be returned if `this.required == true`.

if (value.length > this.max)
return new invalid_1.Invalid(`Maximum ${this.max} characters`);
return new message_1.Invalid(`Maximum ${this.max} characters`);
// Check format.
if (!R_EMAIL.test(value))
return new invalid_1.Invalid("Invalid email format");
return new message_1.Invalid("Invalid email format");
// Return email.

@@ -69,0 +69,0 @@ return value;

import type { Schema, SchemaOptions } from "../Schema";
import type { NullIfOptional } from "../types";
import { Invalid } from "../helpers/invalid";
import { Invalid } from "../helpers/message";
/** Coerce an unknown value to a key (if possible). */

@@ -5,0 +5,0 @@ export declare function coerceKey(value: unknown): string | null | Invalid;

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.key = exports.KeySchema = exports.coerceKey = void 0;
const invalid_1 = require("../helpers/invalid");
const message_1 = require("../helpers/message");
/** Coerce an unknown value to a key (if possible). */

@@ -13,3 +13,3 @@ function coerceKey(value) {

return value.trim() || null;
return new invalid_1.Invalid("Must be string or null");
return new message_1.Invalid("Must be string or null");
}

@@ -38,3 +38,3 @@ exports.coerceKey = coerceKey;

const value = coerceKey(unsafeValue);
if (value instanceof invalid_1.Invalid)
if (value instanceof message_1.Invalid)
return value;

@@ -45,3 +45,3 @@ // Null means 'no key'

if (this.required)
return new invalid_1.Required("Required");
return new message_1.Required("Required");
// Return.

@@ -53,3 +53,3 @@ // We know this type assertion is sound because `null` can never be returned if `this.required == true`.

if (!this.match.test(value))
return new invalid_1.Invalid("Invalid key format");
return new message_1.Invalid("Invalid key format");
// Return key.

@@ -56,0 +56,0 @@ return value;

import type { Schema, SchemaOptions, SchemaType } from "../Schema";
import { Invalid } from "../helpers/invalid";
import { Invalid } from "../helpers/message";
/** Coerce an unknown value to a map (if possible). */

@@ -4,0 +4,0 @@ export declare function coerceMap(value: unknown): Record<string, unknown> | Invalid;

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.map = exports.MapSchema = exports.coerceMap = void 0;
const invalid_1 = require("../helpers/invalid");
const message_1 = require("../helpers/message");
/** Coerce an unknown value to a map (if possible). */

@@ -11,3 +11,3 @@ function coerceMap(value) {

return value;
return new invalid_1.Invalid("Must be object");
return new message_1.Invalid("Must be object");
}

@@ -39,3 +39,3 @@ exports.coerceMap = coerceMap;

const unsafeObject = coerceMap(unsafeValue);
if (unsafeObject instanceof invalid_1.Invalid)
if (unsafeObject instanceof message_1.Invalid)
return unsafeObject;

@@ -48,3 +48,3 @@ // Get number of properties.

if (this.required)
return new invalid_1.Required("Required");
return new message_1.Required("Required");
// Return empty object.

@@ -56,5 +56,5 @@ // We know this type assertion is sound because we know value is empty.

if (typeof this.min === "number" && length < this.min)
return new invalid_1.Invalid(`Minimum ${this.min} items`);
return new message_1.Invalid(`Minimum ${this.min} items`);
if (typeof this.max === "number" && length > this.max)
return new invalid_1.Invalid(`Maximum ${this.max} items`);
return new message_1.Invalid(`Maximum ${this.max} items`);
// Check value against against `this.items`

@@ -67,3 +67,3 @@ let changed = false;

const safeProp = this.items.validate(unsafeProp);
if (safeProp instanceof invalid_1.Invalid) {
if (safeProp instanceof message_1.Invalid) {
invalid = true;

@@ -80,3 +80,3 @@ invalids[key] = safeProp;

if (invalid)
return new invalid_1.Invalid("Invalid items", invalids);
return new message_1.Invalid("Invalid items", invalids);
// Return immuatably (return output if changes were made, or exact input otherwise).

@@ -83,0 +83,0 @@ return (changed ? output : unsafeObject);

import type { Schema, SchemaOptions } from "../Schema";
import type { NullIfOptional } from "../types";
import { Invalid } from "../helpers/invalid";
import { Invalid } from "../helpers/message";
/** Coerce an unknown value to a number (if possible). */

@@ -5,0 +5,0 @@ export declare function coerceNumber(value: unknown): number | null | Invalid;

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.number = exports.NumberSchema = exports.coerceNumber = void 0;
const invalid_1 = require("../helpers/invalid");
const message_1 = require("../helpers/message");
const number_1 = require("../helpers/number");

@@ -16,5 +16,5 @@ /** Coerce an unknown value to a number (if possible). */

return numValue;
return new invalid_1.Invalid("Must be numeric");
return new message_1.Invalid("Must be numeric");
}
return new invalid_1.Invalid("Must be number or null");
return new message_1.Invalid("Must be number or null");
}

@@ -41,3 +41,3 @@ exports.coerceNumber = coerceNumber;

let value = coerceNumber(unsafeValue);
if (value instanceof invalid_1.Invalid)
if (value instanceof message_1.Invalid)
return value;

@@ -48,3 +48,3 @@ // Null means 'no number'

if (this.required)
return new invalid_1.Required("Required");
return new message_1.Required("Required");
// Return null.

@@ -56,8 +56,8 @@ // We know this type assertion is sound because `null` can never be returned if `this.required == true`.

if (!Number.isFinite(value))
return new invalid_1.Invalid("Must be finite number");
return new message_1.Invalid("Must be finite number");
// Check min and max.
if (typeof this.max === "number" && value > this.max)
return new invalid_1.Invalid(`Maximum ${this.max}`);
return new message_1.Invalid(`Maximum ${this.max}`);
if (typeof this.min === "number" && value < this.min)
return new invalid_1.Invalid(`Minimum ${this.min}`);
return new message_1.Invalid(`Minimum ${this.min}`);
// Round to step.

@@ -70,7 +70,7 @@ if (typeof this.step === "number")

if (!this.options.includes(value))
return new invalid_1.Invalid("Unknown value");
return new message_1.Invalid("Unknown value");
}
else {
if (!(value.toString() in this.options))
return new invalid_1.Invalid("Unknown value");
return new message_1.Invalid("Unknown value");
}

@@ -77,0 +77,0 @@ }

import type { Schema, Schemas, SchemasType, SchemaOptions } from "../Schema";
import type { NullIfOptional, UnknownObject } from "../types";
import { Invalid } from "../helpers/invalid";
import { Invalid } from "../helpers/message";
/** Coerce an unknown value to an object (if possible). */

@@ -5,0 +5,0 @@ export declare function coerceObject(value: unknown): UnknownObject | null | Invalid;

@@ -5,3 +5,3 @@ "use strict";

exports.object = exports.ObjectSchema = exports.coerceObject = void 0;
const invalid_1 = require("../helpers/invalid");
const message_1 = require("../helpers/message");
/** Coerce an unknown value to an object (if possible). */

@@ -13,3 +13,3 @@ function coerceObject(value) {

return value;
return new invalid_1.Invalid("Must be object");
return new message_1.Invalid("Must be object");
}

@@ -35,3 +35,3 @@ exports.coerceObject = coerceObject;

const unsafeObj = coerceObject(unsafeValue);
if (unsafeObj instanceof invalid_1.Invalid)
if (unsafeObj instanceof message_1.Invalid)
return unsafeObj;

@@ -42,3 +42,3 @@ // Null means 'no object'

if (this.required)
return new invalid_1.Required("Required");
return new message_1.Required("Required");
// Return.

@@ -56,3 +56,3 @@ return null;

const safeProp = schema.validate(unsafeProp);
if (safeProp instanceof invalid_1.Invalid) {
if (safeProp instanceof message_1.Invalid) {
invalid = true;

@@ -72,3 +72,3 @@ invalids[key] = safeProp;

if (invalid)
return new invalid_1.Invalid("Invalid format", invalids);
return new message_1.Invalid("Invalid format", invalids);
// Return immuatably (return output if changes were made, or exact input otherwise).

@@ -75,0 +75,0 @@ return (changed ? safeObj : unsafeObj);

import type { Schema, SchemaOptions } from "../Schema";
import type { NullIfOptional } from "../types";
import { Invalid } from "../helpers/invalid";
import { Invalid } from "../helpers/message";
/** Coerce an unknown value into a phone number (if possible). */

@@ -5,0 +5,0 @@ export declare function coercePhone(value: unknown): string | null | Invalid;

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.phone = exports.PhoneSchema = exports.coercePhone = void 0;
const invalid_1 = require("../helpers/invalid");
const message_1 = require("../helpers/message");
/**

@@ -23,3 +23,3 @@ * Clean a phone number string by removing characters that aren't digits.

return cleanPhone(value) || null; // Return the clean phone number (also convert empty string to null).
return new invalid_1.Invalid("Must be string or null");
return new message_1.Invalid("Must be string or null");
}

@@ -50,3 +50,3 @@ exports.coercePhone = coercePhone;

const value = coercePhone(unsafeValue);
if (value instanceof invalid_1.Invalid)
if (value instanceof message_1.Invalid)
return value;

@@ -57,3 +57,3 @@ // Null means 'no phone'

if (this.required)
return new invalid_1.Required("Required");
return new message_1.Required("Required");
// Return null.

@@ -65,6 +65,6 @@ // We know this type assertion is sound because `null` can never be returned if `this.required == true`.

if (value.length > this.max)
return new invalid_1.Invalid(`Maximum ${this.max} characters`);
return new message_1.Invalid(`Maximum ${this.max} characters`);
// Check format.
if (!R_PHONE.test(value))
return new invalid_1.Invalid("Invalid phone number (must be an international number starting with `+` plus)");
return new message_1.Invalid("Invalid phone number (must be an international number starting with `+` plus)");
// Return phone.

@@ -71,0 +71,0 @@ return value;

import type { Schema, SchemaOptions } from "../Schema";
import type { EmptyIfOptional } from "../types";
import { Invalid } from "../helpers/invalid";
import { Invalid } from "../helpers/message";
/** Coerce an unknown value to a string (if possible). */

@@ -5,0 +5,0 @@ export declare function coerceString(value: unknown): string | Invalid;

@@ -5,3 +5,3 @@ "use strict";

exports.string = exports.StringSchema = exports.coerceString = void 0;
const invalid_1 = require("../helpers/invalid");
const message_1 = require("../helpers/message");
/** Coerce an unknown value to a string (if possible). */

@@ -15,3 +15,3 @@ function coerceString(value) {

return ""; // Convert falsy to empty string.
return new invalid_1.Invalid("Must be string");
return new message_1.Invalid("Must be string");
}

@@ -58,3 +58,3 @@ exports.coerceString = coerceString;

const uncleanValue = coerceString(unsafeValue);
if (uncleanValue instanceof invalid_1.Invalid)
if (uncleanValue instanceof message_1.Invalid)
return uncleanValue;

@@ -67,3 +67,3 @@ // Strip control characters.

if (this.required)
return new invalid_1.Required("Required");
return new message_1.Required("Required");
// Return.

@@ -74,6 +74,6 @@ return "";

if (typeof this.max === "number" && value.length > this.max)
return new invalid_1.Invalid(`Maximum ${this.max} characters`);
return new message_1.Invalid(`Maximum ${this.max} characters`);
// Check min.
if (typeof this.min === "number" && value.length < this.min)
return new invalid_1.Invalid(`Minimum ${this.min} characters`);
return new message_1.Invalid(`Minimum ${this.min} characters`);
// Check enum format.

@@ -83,7 +83,7 @@ if (this.options) {

if (!this.options.includes(value))
return new invalid_1.Invalid("Unknown value");
return new message_1.Invalid("Unknown value");
}
else {
if (!(value in this.options))
return new invalid_1.Invalid("Unknown value");
return new message_1.Invalid("Unknown value");
}

@@ -93,3 +93,3 @@ }

if (this.match && !this.match.test(value))
return new invalid_1.Invalid("Invalid format");
return new message_1.Invalid("Invalid format");
// Return string.

@@ -96,0 +96,0 @@ // This type assertion is okay because the checks above make it so.

import type { NullIfOptional } from "../types";
import type { Schema, SchemaOptions } from "../Schema";
import { Invalid } from "../helpers/invalid";
import { Invalid } from "../helpers/message";
/** Coerce an unknown value to a URL (if possible). */

@@ -5,0 +5,0 @@ export declare function coerceUrl(value: unknown): string | null | Invalid;

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.url = exports.UrlSchema = exports.coerceUrl = void 0;
const invalid_1 = require("../helpers/invalid");
const message_1 = require("../helpers/message");
/** Coerce an unknown value to a URL (if possible). */

@@ -11,3 +11,3 @@ function coerceUrl(value) {

return value.trim() || null;
return new invalid_1.Invalid("Must be string or null");
return new message_1.Invalid("Must be string or null");
}

@@ -39,3 +39,3 @@ exports.coerceUrl = coerceUrl;

const value = coerceUrl(unsafeValue);
if (value instanceof invalid_1.Invalid)
if (value instanceof message_1.Invalid)
return value;

@@ -46,3 +46,3 @@ // Null means 'no URL'

if (this.required)
return new invalid_1.Required("Required");
return new message_1.Required("Required");
// Return null.

@@ -60,12 +60,12 @@ // We know this type assertion is sound because `null` can never be returned if `this.required == true`.

// Definitely not valid.
return new invalid_1.Invalid("Invalid URL format");
return new message_1.Invalid("Invalid URL format");
}
// Check length again.
if (u.href.length > this.max)
return new invalid_1.Invalid(`Maximum ${this.max} characters`);
return new message_1.Invalid(`Maximum ${this.max} characters`);
// Check scheme and domain exist in whitelists.
if (!this.schemes.includes(u.protocol))
return new invalid_1.Invalid(`Scheme "${u.protocol}" is not allowed`);
return new message_1.Invalid(`Scheme "${u.protocol}" is not allowed`);
if (this.hosts && !this.hosts.includes(u.host))
return new invalid_1.Invalid(`Domain "${u.host}" is not allowed`);
return new message_1.Invalid(`Domain "${u.host}" is not allowed`);
// Check domain.

@@ -75,3 +75,3 @@ if (u.host.length) {

if (u.host.length > 253)
return new invalid_1.Invalid(`Invalid URL format`);
return new message_1.Invalid(`Invalid URL format`);
// Each host segment is no more than 63 characters.

@@ -81,3 +81,3 @@ const bits = u.host.split(".");

if (bits[i].length > 63)
return new invalid_1.Invalid(`Invalid URL format`);
return new message_1.Invalid(`Invalid URL format`);
}

@@ -84,0 +84,0 @@ // Return the normalised URL.

{
"name": "schemaglobin",
"description": "Validate user-entered data.",
"version": "5.4.3",
"version": "5.5.0",
"repository": "https://github.com/dhoulb/schemaglobin",

@@ -6,0 +6,0 @@ "author": "Dave Houlbrooke <dave@shax.com>",

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc