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

json-schemify

Package Overview
Dependencies
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

json-schemify - npm Package Compare versions

Comparing version 0.0.12 to 0.0.13

dist/types.d.ts

17

dist/schema.d.ts

@@ -0,10 +1,15 @@

import { Json, Options } from 'types';
import { Schema } from './schema.interface';
/**
* Return a generated schema as a json object
* @param json A valid json object or array
* Returns a Json schema object
* @param json A valid json structure
* @param options Options to pass
*/
export declare function schemify(json: {} | Array<any>, options?: {
id?: string;
title?: string;
}): Schema;
export declare function schemify(json: Json, options?: Options): Schema;
/**
* Write
* @param json A valid json structure
* @param filename The path of the output file to write to
* @param options Options to pass
*/
export declare function writeSchema(json: Json, filepath: string, options?: Options): void;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.schemify = void 0;
exports.writeSchema = exports.schemify = void 0;
const fs = require("fs");
const path = require("path");
const any_1 = require("./types/any");
/**
* Return a generated schema as a json object
* @param json A valid json object or array
* Returns a Json schema object
* @param json A valid json structure
* @param options Options to pass

@@ -15,1 +17,23 @@ */

exports.schemify = schemify;
/**
* Write
* @param json A valid json structure
* @param filename The path of the output file to write to
* @param options Options to pass
*/
function writeSchema(json, filepath, options) {
const schema = schemify(json, options);
if (!fs.existsSync(path.dirname(filepath))) {
fs.mkdirSync(path.dirname(filepath));
}
if (options === null || options === void 0 ? void 0 : options.prettyPrint) {
fs.writeFileSync(filepath, JSON.stringify(schema, null, 2));
}
else {
fs.writeFileSync(filepath, JSON.stringify(schema));
}
if (!(options === null || options === void 0 ? void 0 : options.silent)) {
console.log(`[json-schemify] Json schema written to ${filepath}`);
}
}
exports.writeSchema = writeSchema;

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

export declare function parse(json: any | Array<any>): import("..").Schema;
import { Json } from 'types';
export declare function parse(json: Json | number | string): import("..").Schema;

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

function getItems(array) {
const arrayOfTypes = lodash_1.uniqWith(array.map(item => {
const arrayOfTypes = lodash_1.uniqWith(array.map((item) => {
return any_1.parse(item);

@@ -20,3 +20,3 @@ }), lodash_1.isEqual);

const compareObjects = [];
arrayOfTypes.forEach(type => {
arrayOfTypes.forEach((type) => {
if (type.properties) {

@@ -35,6 +35,10 @@ compareObjects.push(type.properties);

return {
type: objValue.type.includes(srcValue.type) ? objValue.type : [...objValue.type, srcValue.type],
type: objValue.type.includes(srcValue.type)
? objValue.type
: [...objValue.type, srcValue.type],
};
}
else if (objValue.type !== 'object' && objValue.type !== 'array' && !lodash_1.isEqual(objValue, srcValue)) {
else if (objValue.type !== 'object' &&
objValue.type !== 'array' &&
!lodash_1.isEqual(objValue, srcValue)) {
return { type: [objValue.type, srcValue.type] };

@@ -62,3 +66,3 @@ }

const union = new Set(allKeys);
return objects.every(object => union.size === Object.keys(object).length);
return objects.every((object) => union.size === Object.keys(object).length);
}

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

import { Json } from 'types';
import { Schema } from '../schema.interface';
export declare function parseObject(object: {}): Schema;
export declare function parseObject(object: Json): Schema;

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

if (lodash_1.isObject(objToCompare)) {
objects.forEach(obj => {
objects.forEach((obj) => {
if (lodash_1.isObject(obj) && !compareKeys(objToCompare, obj)) {

@@ -33,0 +33,0 @@ match = false;

{
"name": "json-schemify",
"version": "0.0.12",
"version": "0.0.13",
"description": "Converts a JSON structure to a valid JSON Schema object.",

@@ -11,3 +11,3 @@ "main": "dist/index.js",

"scripts": {
"lint": "tslint --project tsconfig.json",
"lint": "eslint ./src --ext .ts",
"build": "rm -rf dist && tsc",

@@ -45,11 +45,15 @@ "release": "standard-version",

"@types/node": "^16.4.13",
"@types/tmp": "^0.2.1",
"@typescript-eslint/eslint-plugin": "^4.29.0",
"@typescript-eslint/parser": "^4.29.0",
"ajv": "^8.6.2",
"ajv-formats": "^2.1.0",
"eslint": "^7.32.0",
"jest": "^27.0.6",
"standard-version": "^9.3.1",
"tmp": "^0.2.1",
"ts-jest": "^27.0.4",
"ts-node": "^10.1.0",
"tslint": "^5.20.1",
"typescript": "^4.3.5"
}
}

@@ -21,19 +21,61 @@ # json-schemify

```js
const { schemify } = require('json-schemify');
const { writeSchema } = require('json-schemify');
```
or
## API
### writeSchema
**writeSchema**(`json`, `filepath`, `options`)
Writes to a JSON schema output file.
```js
import { schemify } from 'json-schemify';
const json= {
firstName: 'John',
lastName: 'Doe',
age: 21,
}
writeSchema(json, 'schema.json');
```
## API
#### Params
The package exposes a single method:
##### `json`
### schemify(json, options)
Any valid JSON.
##### `filepath`
The filepath of the file to write.
##### `options`
| Option | Description |
| ------------ | -------------------------------- |
| id? | The \$id property of the schema |
| title? | The title property of the schema |
| prettyPrint? | Pretty print Json output |
___
### schemify
**schemify**(`json`, `options`)
Returns the JSON schema object (rather than writing to file).
```js
const schema = schemify(json, options);
const json= {
firstName: 'John',
lastName: 'Doe',
age: 21,
}
const schema = schemify(json);
// do something with schema
console.log(schema);
```

@@ -43,14 +85,12 @@

#### `json`
##### `json`
Any valid JSON.
#### `options`
##### `options`
Options object (all options are optional).
| Option | Description |
| ------ | -------------------------------- |
| id | The \$id property of the schema |
| title | The title property of the schema |
| id? | The \$id property of the schema |
| title? | The title property of the schema |

@@ -61,13 +101,10 @@ #### Returns

## Basic Example
## Example
This example returns a schema at the most basic level:
This example returns a basic schema.
### API
### Json
```js
const { schemify } = require('json-schemify');
const json = {
{
firstName: 'John',

@@ -77,7 +114,2 @@ lastName: 'Doe',

};
const schema = schemify(json);
// do something with the schema
console.log(schema);
```

@@ -89,8 +121,8 @@

{
$schema: "http://json-schema.org/draft-07/schema#",
type: "object",
properties: {
firstName: { "type": "string" },
lastName: { "type": "string" },
age: { "type": "integer" }
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"firstName": { "type": "string" },
"lastName": { "type": "string" },
"age": { "type": "integer" }
}

@@ -97,0 +129,0 @@ }

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