Socket
Socket
Sign inDemoInstall

scrubbr

Package Overview
Dependencies
67
Maintainers
1
Versions
16
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.0.1-alpha.3 to 0.0.1-alpha.4

test/moment.schema.json

13

dist/Logger.d.ts

@@ -10,8 +10,9 @@ export declare enum LogLevel {

logLevel: LogLevel;
constructor(logLevel: LogLevel);
private getIndent;
info(message: string, indent?: number): void;
error(message: string, indent?: number): void;
warn(message: string, indent?: number): void;
debug(message: string, indent?: number): void;
nestingString: string | boolean;
constructor(logLevel: LogLevel, nesting: boolean | string);
private getNesting;
info(message: string, nestingLevel?: number): void;
error(message: string, nestingLevel?: number): void;
warn(message: string, nestingLevel?: number): void;
debug(message: string, nestingLevel?: number): void;
}

@@ -13,35 +13,45 @@ "use strict";

var Logger = /** @class */ (function () {
function Logger(logLevel) {
function Logger(logLevel, nesting) {
this.nestingString = false;
this.logLevel = logLevel;
if (nesting === true) {
this.nestingString = ' ';
}
else if (typeof nesting == 'string') {
this.nestingString = nesting;
}
}
Logger.prototype.getIndent = function (num) {
return Array(num).fill(' ').join('');
Logger.prototype.getNesting = function (num) {
if (!this.nestingString) {
return '';
}
return Array(num).fill(this.nestingString).join('');
};
Logger.prototype.info = function (message, indent) {
if (indent === void 0) { indent = 0; }
Logger.prototype.info = function (message, nestingLevel) {
if (nestingLevel === void 0) { nestingLevel = 0; }
if (this.logLevel < LogLevel.INFO) {
return;
}
console.log("" + this.getIndent(indent) + message);
console.log("" + this.getNesting(nestingLevel) + message);
};
Logger.prototype.error = function (message, indent) {
if (indent === void 0) { indent = 0; }
Logger.prototype.error = function (message, nestingLevel) {
if (nestingLevel === void 0) { nestingLevel = 0; }
if (this.logLevel < LogLevel.ERROR) {
return;
}
console.error("" + this.getIndent(indent) + message);
console.error("" + this.getNesting(nestingLevel) + message);
};
Logger.prototype.warn = function (message, indent) {
if (indent === void 0) { indent = 0; }
Logger.prototype.warn = function (message, nestingLevel) {
if (nestingLevel === void 0) { nestingLevel = 0; }
if (this.logLevel < LogLevel.WARN) {
return;
}
console.warn("" + this.getIndent(indent) + message);
console.warn("" + this.getNesting(nestingLevel) + message);
};
Logger.prototype.debug = function (message, indent) {
if (indent === void 0) { indent = 0; }
Logger.prototype.debug = function (message, nestingLevel) {
if (nestingLevel === void 0) { nestingLevel = 0; }
if (this.logLevel < LogLevel.DEBUG) {
return;
}
console.debug("" + this.getIndent(indent) + message);
console.debug("" + this.getNesting(nestingLevel) + message);
};

@@ -48,0 +58,0 @@ return Logger;

@@ -8,2 +8,3 @@ import { JSONSchema7 } from 'json-schema';

logLevel?: LogLevel;
logNesting?: boolean | string;
};

@@ -21,3 +22,3 @@ export default class Scrubbr {

*/
constructor(schema: string | JSONSchema7, options?: ScrubbrOptions, typeSerializers?: Map<string, TypeSerializer[]>, pathSerializers?: PathSerializer[]);
constructor(schema: string | JSONSchema7, options?: ScrubbrOptions);
/**

@@ -60,15 +61,23 @@ * Create new scrubber with the same options and custom serializers

/**
* Serialize a single node, recursively
* Traverse into a node of data on an object to serialize.
* @param {object} node: The data object to start from
* @param {ScrubbrState} state - The serializing state.
*/
private walkData;
/**
* Serialize an object in the data structure
* Serialize all the properties of an object.
* @param {object} node - The object to serialize
* @param {ScrubbrState} state - The serializing state.
*/
private walkObjectNode;
/**
* Serialize an array node
* Serialize all the items of an array.
* @param {any} node[] - The array to serialize
* @param {ScrubbrState} state - The serializing state.
*/
private walkArrayNode;
/**
* Serialize a node of data
* Serialize a single piece of data.
* @param {any} data - The data to serialize
* @param {ScrubbrState} state - The serializing state.
*/

@@ -79,7 +88,7 @@ private serializeNode;

*/
private getNodeType;
private getTypeName;
/**
* Set the schema definition in the state
*/
private setStateSchema;
private setStateSchemaDefinition;
/**

@@ -86,0 +95,0 @@ * Run serializers on the data path

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

*/
function Scrubbr(schema, options, typeSerializers, pathSerializers) {
function Scrubbr(schema, options) {
if (options === void 0) { options = {}; }

@@ -77,10 +77,4 @@ this.schema = {};

this.options = options;
this.logger = new Logger_1.Logger(options.logLevel || Logger_1.LogLevel.NONE);
this.logger = new Logger_1.Logger(options.logLevel || Logger_1.LogLevel.NONE, options.logNesting || true);
this.loadSchema(schema);
if (typeSerializers) {
this.typeSerializers = new Map(typeSerializers);
}
if (pathSerializers) {
this.pathSerializers = pathSerializers;
}
}

@@ -91,3 +85,12 @@ /**

Scrubbr.prototype.clone = function (options) {
return new Scrubbr(this.schema, options, this.typeSerializers, this.pathSerializers);
var cloned = new Scrubbr(this.schema, options);
this.pathSerializers.forEach(function (serializerFn) {
cloned.addPathSerializer(serializerFn);
});
this.typeSerializers.forEach(function (serializers, typeName) {
serializers.forEach(function (serializerFn) {
cloned.addTypeSerializer(typeName, serializerFn);
});
});
return cloned;
};

@@ -182,3 +185,5 @@ /**

/**
* Serialize a single node, recursively
* Traverse into a node of data on an object to serialize.
* @param {object} node: The data object to start from
* @param {ScrubbrState} state - The serializing state.
*/

@@ -209,3 +214,5 @@ Scrubbr.prototype.walkData = function (node, state) {

/**
* Serialize an object in the data structure
* Serialize all the properties of an object.
* @param {object} node - The object to serialize
* @param {ScrubbrState} state - The serializing state.
*/

@@ -229,3 +236,3 @@ Scrubbr.prototype.walkObjectNode = function (node, state) {

propPath = "" + pathPrefix + name_1;
this.logger.debug(propPath, state.nesting);
this.logger.debug("[PATH] " + propPath, state.nesting);
propState = state.nodeState(propPath, propSchema);

@@ -252,3 +259,5 @@ // Property not defined in the schema, do not serialize property

/**
* Serialize an array node
* Serialize all the items of an array.
* @param {any} node[] - The array to serialize
* @param {ScrubbrState} state - The serializing state.
*/

@@ -276,3 +285,3 @@ Scrubbr.prototype.walkArrayNode = function (node, state) {

itemPath = state.path + "[" + i + "]";
this.logger.debug(itemPath, state.nesting);
this.logger.debug("[PATH] " + itemPath, state.nesting);
itemState = state.nodeState(itemPath, itemSchema);

@@ -299,16 +308,20 @@ // Skip items past the tuple length

/**
* Serialize a node of data
* Serialize a single piece of data.
* @param {any} data - The data to serialize
* @param {ScrubbrState} state - The serializing state.
*/
Scrubbr.prototype.serializeNode = function (data, state) {
return __awaiter(this, void 0, void 0, function () {
var schemaType, schemaDef;
var originalDef, schemaType, schemaDef;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
schemaType = this.getNodeType(state);
originalDef = state.schemaDef;
schemaType = this.getTypeName(state.schemaDef, state);
if (schemaType) {
this.setStateSchema(schemaType, state);
state = this.setStateSchemaDefinition(schemaType, state);
}
return [4 /*yield*/, this.runPathSerializers(data, state)];
case 1:
// Run serializers
data = _a.sent();

@@ -320,2 +333,4 @@ return [4 /*yield*/, this.runTypeSerializers(data, state)];

if (schemaDef &&
schemaType &&
originalDef !== schemaDef &&
!schemaDef.properties &&

@@ -334,5 +349,4 @@ (schemaDef.allOf || schemaDef.anyOf || schemaDef.oneOf)) {

*/
Scrubbr.prototype.getNodeType = function (state) {
Scrubbr.prototype.getTypeName = function (schema, state) {
var _this = this;
var schema = state.schemaDef;
if (!schema) {

@@ -348,3 +362,3 @@ return null;

}
// Get all types defined
// Get all the types we have definitions for
var foundTypes = new Map();

@@ -391,5 +405,2 @@ schemaList.forEach(function (schemaRef) {

}
if (chosenType) {
this.setStateSchema(chosenType, state);
}
return chosenType;

@@ -400,3 +411,3 @@ };

*/
Scrubbr.prototype.setStateSchema = function (schemaType, state) {
Scrubbr.prototype.setStateSchemaDefinition = function (schemaType, state) {
var primitiveTypes = [

@@ -410,12 +421,15 @@ 'string',

];
if (primitiveTypes.includes(schemaType)) {
state.schemaType = schemaType;
}
else {
var schemaDef = this.getSchemaForType(schemaType);
if (!schemaDef) {
throw new Error("Could not find a type definition for '" + schemaType + "'");
if (schemaType) {
if (primitiveTypes.includes(schemaType)) {
state.schemaType = schemaType;
state.schemaDef = {};
}
state.schemaType = schemaType;
state.schemaDef = schemaDef;
else {
var schemaDef = this.getSchemaForType(schemaType);
if (!schemaDef) {
throw new Error("Could not find a type definition for '" + schemaType + "'");
}
state.schemaType = schemaType;
state.schemaDef = schemaDef;
}
}

@@ -440,3 +454,3 @@ return state;

_this.logger.debug("Overriding type: '" + serialized.typeName + "'", state.nesting);
_this.setStateSchema(serialized.typeName, state);
state = _this.setStateSchemaDefinition(serialized.typeName, state);
return data;

@@ -480,3 +494,3 @@ }

this.logger.debug("Overriding type: '" + serialized.typeName + "'", state.nesting);
this.setStateSchema(serialized.typeName, state);
state = this.setStateSchemaDefinition(serialized.typeName, state);
return [4 /*yield*/, this.runTypeSerializers(dataNode, state)];

@@ -483,0 +497,0 @@ case 3: return [2 /*return*/, _a.sent()];

@@ -13,2 +13,6 @@ import { JSONSchema7 } from 'json-schema';

/**
* Create a shallow copy of this state
*/
clone(): this;
/**
* Create a child state off of this one

@@ -15,0 +19,0 @@ */

"use strict";
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
Object.defineProperty(exports, "__esModule", { value: true });

@@ -18,2 +29,8 @@ exports.ScrubbrState = void 0;

/**
* Create a shallow copy of this state
*/
ScrubbrState.prototype.clone = function () {
return __assign({}, this);
};
/**
* Create a child state off of this one

@@ -20,0 +37,0 @@ */

{
"name": "scrubbr",
"version": "0.0.1-alpha.3",
"version": "0.0.1-alpha.4",
"description": "Serialize and sanitize JSON data using TypeScript.",

@@ -23,2 +23,3 @@ "repository": "https://github.com/jgillick/scrubbr",

"jest": "^27.0.4",
"moment": "^2.29.1",
"ts-jest": "^27.0.3",

@@ -25,0 +26,0 @@ "typescript": "^4.3.2"

@@ -51,3 +51,3 @@ # Scrubbr

// Serialize the data based on the UserList type defined in schema.ts
return await scrubbr.serialize(data, 'UserList');
return await scrubbr.serialize('UserList', data);
}

@@ -111,3 +111,3 @@

};
const serialized = await scrubbr.serialize(data, 'PostList', context);
const serialized = await scrubbr.serialize('PostList', data, context);
```

@@ -138,3 +138,3 @@

};
const serialized = await scrubbr.serialize(data, 'PostList', context);
const serialized = await scrubbr.serialize('PostList', data, context);
```

@@ -164,3 +164,3 @@

// Serialize
const output = await scrubbr.serialize(data, 'UserList');
const output = await scrubbr.serialize('UserList', data);
const jsonSchema = scrubbr.getSchema();

@@ -167,0 +167,0 @@

@@ -11,38 +11,47 @@ export enum LogLevel {

logLevel: LogLevel;
nestingString: string | boolean = false;
constructor(logLevel: LogLevel) {
constructor(logLevel: LogLevel, nesting: boolean | string) {
this.logLevel = logLevel;
if (nesting === true) {
this.nestingString = ' ';
} else if (typeof nesting == 'string') {
this.nestingString = nesting;
}
}
private getIndent(num: number): string {
return Array(num).fill(' ').join('');
private getNesting(num: number): string {
if (!this.nestingString) {
return '';
}
return Array(num).fill(this.nestingString).join('');
}
info(message: string, indent: number = 0) {
info(message: string, nestingLevel: number = 0) {
if (this.logLevel < LogLevel.INFO) {
return;
}
console.log(`${this.getIndent(indent)}${message}`);
console.log(`${this.getNesting(nestingLevel)}${message}`);
}
error(message: string, indent: number = 0) {
error(message: string, nestingLevel: number = 0) {
if (this.logLevel < LogLevel.ERROR) {
return;
}
console.error(`${this.getIndent(indent)}${message}`);
console.error(`${this.getNesting(nestingLevel)}${message}`);
}
warn(message: string, indent: number = 0) {
warn(message: string, nestingLevel: number = 0) {
if (this.logLevel < LogLevel.WARN) {
return;
}
console.warn(`${this.getIndent(indent)}${message}`);
console.warn(`${this.getNesting(nestingLevel)}${message}`);
}
debug(message: string, indent: number = 0) {
debug(message: string, nestingLevel: number = 0) {
if (this.logLevel < LogLevel.DEBUG) {
return;
}
console.debug(`${this.getIndent(indent)}${message}`);
console.debug(`${this.getNesting(nestingLevel)}${message}`);
}
}

@@ -20,2 +20,3 @@ import * as fs from 'fs';

logLevel?: LogLevel;
logNesting?: boolean | string;
};

@@ -37,18 +38,9 @@

*/
constructor(
schema: string | JSONSchema7,
options: ScrubbrOptions = {},
typeSerializers?: Map<string, TypeSerializer[]>,
pathSerializers?: PathSerializer[]
) {
constructor(schema: string | JSONSchema7, options: ScrubbrOptions = {}) {
this.options = options;
this.logger = new Logger(options.logLevel || LogLevel.NONE);
this.logger = new Logger(
options.logLevel || LogLevel.NONE,
options.logNesting || true
);
this.loadSchema(schema);
if (typeSerializers) {
this.typeSerializers = new Map(typeSerializers);
}
if (pathSerializers) {
this.pathSerializers = pathSerializers;
}
}

@@ -60,8 +52,14 @@

clone(options: ScrubbrOptions): Scrubbr {
return new Scrubbr(
this.schema,
options,
this.typeSerializers,
this.pathSerializers
);
const cloned = new Scrubbr(this.schema, options);
this.pathSerializers.forEach((serializerFn) => {
cloned.addPathSerializer(serializerFn);
});
this.typeSerializers.forEach((serializers, typeName) => {
serializers.forEach((serializerFn) => {
cloned.addTypeSerializer(typeName, serializerFn);
});
});
return cloned;
}

@@ -165,3 +163,5 @@

/**
* Serialize a single node, recursively
* Traverse into a node of data on an object to serialize.
* @param {object} node: The data object to start from
* @param {ScrubbrState} state - The serializing state.
*/

@@ -182,3 +182,5 @@ private async walkData(node: Object, state: ScrubbrState): Promise<Object> {

/**
* Serialize an object in the data structure
* Serialize all the properties of an object.
* @param {object} node - The object to serialize
* @param {ScrubbrState} state - The serializing state.
*/

@@ -199,4 +201,3 @@ private async walkObjectNode(

const propPath = `${pathPrefix}${name}`;
this.logger.debug(propPath, state.nesting);
this.logger.debug(`[PATH] ${propPath}`, state.nesting);
const propState = state.nodeState(propPath, propSchema);

@@ -219,8 +220,7 @@

/**
* Serialize an array node
* Serialize all the items of an array.
* @param {any} node[] - The array to serialize
* @param {ScrubbrState} state - The serializing state.
*/
private async walkArrayNode(
node: Object[],
state: ScrubbrState
): Promise<Object> {
private async walkArrayNode(node: any[], state: ScrubbrState): Promise<any> {
const schema = state.schemaDef;

@@ -241,3 +241,3 @@ const listSchema = schema.items as JSONSchema7 | JSONSchema7[];

const itemPath = `${state.path}[${i}]`;
this.logger.debug(itemPath, state.nesting);
this.logger.debug(`[PATH] ${itemPath}`, state.nesting);
const itemState = state.nodeState(itemPath, itemSchema as JSONSchema7);

@@ -258,19 +258,25 @@

/**
* Serialize a node of data
* Serialize a single piece of data.
* @param {any} data - The data to serialize
* @param {ScrubbrState} state - The serializing state.
*/
private async serializeNode(
data: Object,
state: ScrubbrState
): Promise<Object> {
const schemaType = this.getNodeType(state);
private async serializeNode(data: any, state: ScrubbrState): Promise<Object> {
const originalDef = state.schemaDef;
// Get typescript type for the schema definition
const schemaType = this.getTypeName(state.schemaDef, state);
if (schemaType) {
this.setStateSchema(schemaType, state);
state = this.setStateSchemaDefinition(schemaType, state);
}
// Run serializers
data = await this.runPathSerializers(data, state);
data = await this.runTypeSerializers(data, state);
// If the type is an alias to a union, walk one level deeper
// If the type definition is an alias to a union, walk one level deeper
const { schemaDef } = state;
if (
schemaDef &&
schemaType &&
originalDef !== schemaDef &&
!schemaDef.properties &&

@@ -292,4 +298,3 @@ (schemaDef.allOf || schemaDef.anyOf || schemaDef.oneOf)

*/
private getNodeType(state: ScrubbrState): string | null {
const schema = state.schemaDef;
private getTypeName(schema: JSONSchema7, state: ScrubbrState): string | null {
if (!schema) {

@@ -308,3 +313,3 @@ return null;

// Get all types defined
// Get all the types we have definitions for
let foundTypes = new Map<string, JSONSchema7>();

@@ -316,2 +321,3 @@ schemaList.forEach((schemaRef) => {

}
let typeName = refPath.replace(/#\/definitions\/(.*)/, '$1');

@@ -363,6 +369,2 @@ typeName = decodeURI(typeName);

if (chosenType) {
this.setStateSchema(chosenType, state);
}
return chosenType;

@@ -374,4 +376,4 @@ }

*/
private setStateSchema(
schemaType: string,
private setStateSchemaDefinition(
schemaType: string | null,
state: ScrubbrState

@@ -388,12 +390,17 @@ ): ScrubbrState {

if (primitiveTypes.includes(schemaType)) {
state.schemaType = schemaType;
} else {
const schemaDef = this.getSchemaForType(schemaType);
if (!schemaDef) {
throw new Error(`Could not find a type definition for '${schemaType}'`);
if (schemaType) {
if (primitiveTypes.includes(schemaType)) {
state.schemaType = schemaType;
state.schemaDef = {};
} else {
const schemaDef = this.getSchemaForType(schemaType);
if (!schemaDef) {
throw new Error(
`Could not find a type definition for '${schemaType}'`
);
}
state.schemaType = schemaType;
state.schemaDef = schemaDef;
}
state.schemaType = schemaType;
state.schemaDef = schemaDef;
}

@@ -428,3 +435,3 @@

);
this.setStateSchema(serialized.typeName, state);
state = this.setStateSchemaDefinition(serialized.typeName, state);
return data;

@@ -472,3 +479,3 @@ }

);
this.setStateSchema(serialized.typeName, state);
state = this.setStateSchemaDefinition(serialized.typeName, state);
return await this.runTypeSerializers(dataNode, state);

@@ -475,0 +482,0 @@ }

@@ -21,2 +21,9 @@ import { JSONSchema7 } from 'json-schema';

/**
* Create a shallow copy of this state
*/
clone() {
return { ...this };
}
/**
* Create a child state off of this one

@@ -23,0 +30,0 @@ */

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc