Skip to content

Commit

Permalink
Merge branch 'beta-v1.1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Harvey committed Feb 8, 2019
2 parents f5a9ffb + b63717e commit 17c22b3
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 170 deletions.
20 changes: 11 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ import {
JSONObject,
JSONArray,
JSONPrimitive,
JSONSchema,
JSONSchemaObject,
JSONLDObject
JSONSchema, // WARNING{DEPRECATED} - use `@types/json-schema` instead
JSONSchemaObject, // WARNING{DEPRECATED} - use `@types/json-schema` instead
JSONLDDocument,
JSONLDObject,
} from '@chharvey/requirejson'

let my_json_object: Promise<JSONValue> = requireJSON('./my-file.jsonld')
Expand All @@ -52,7 +53,7 @@ Asynchronously returns a JSON value that is the result of parsing the file conte
- `filepath`: the path of the file to read, relative to current working directory

#### Returns:
A JSON value parsed from the file contents.
A Promise resolving to a JSON value parsed from the file contents.

### `requireJSONSync(filepath: string): JSONValue`
Synchronously returns a JSON value that is the result of parsing the file contents.
Expand All @@ -66,10 +67,11 @@ A JSON value parsed from the file contents.
## Types
type name | definition | description
----------|------------|------------
JSONValue | `JSONObject or JSONArray or JSONPrimitive` | Any JSON value.
JSONObject | `{[key: string]: JSONValue or undefined;}` | A general JSON object, with string keys and JSONValue values.
JSONValue | `JSONObject or JSONArray or JSONPrimitive` | Any valid [JSON](http://json.org/) value.
JSONObject | `{[key: string]?: JSONValue}` | A general JSON object, with string keys and JSONValue values.
JSONArray | `JSONValue[]` | A JSON array, with JSONValue entries.
JSONPrimitive | `string or number or boolean or null` | A JSON primitive.
JSONSchema | `JSONSchemaObject or boolean` | JSON data that validates against the [JSON-Schema specification](http://json-schema.org/).
JSONSchemaObject | | A JSON object that is a JSON-Schema.
JSONLDObject | | A JSON object that validates against the [JSON-LD specification](https://json-ld.org/).
JSONSchema (DEPRECATED) | `JSONSchema7Definition` | alias of [`JSONSchema7Definition`](https://www.npmjs.com/package/@types/json-schema)[JSON-Schema](http://json-schema.org/).
JSONSchemaObject (DEPRECATED) | `JSONSchema7` | alias of [`JSONSchema7`](https://www.npmjs.com/package/@types/json-schema).
JSONLDDocument | `extends JSONLDObject` | An entire JSON-LD document that validates against the [JSON-LD specification](https://json-ld.org/) — includes optional `@context` and `@graph` properties.
JSONLDObject | `extends JSONObject` | A single object within a JSON-LD document.
15 changes: 10 additions & 5 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,26 @@ import { JSONValue } from './json.d'


/**
* Like node.js `require()`, but can be used on `.jsonld` files.
* @param filepath the relative path of the file to read
* Like node.js `require()`, but can be used on files that might not have a `.json` extension.
* @param filepath the path of the file to read, relative to cwd
* @returns a JSON value that is the result of parsing the file contents
*/
export declare function requireJSON(filepath: string): Promise<JSONValue>;


/**
* Synchronous {@link requireJSONLD}.
* @param filepath the relative path of the file to read
* @param filepath the path of the file to read, relative to cwd
* @returns a JSON value that is the result of parsing the file contents
*/
export declare function requireJSONSync(filepath: string): JSONValue;


export { JSONValue, JSONObject, JSONArray, JSONPrimitive } from './json.d'
export { JSONSchema, JSONSchemaObject } from './json-schema.d'
export { JSONLDObject } from './json-ld.d'
export { JSONLDDocument, JSONLDObject } from './json-ld.d'
export {
JSONSchema,
JSONSchemaObject,
JSONSchema7Definition,
JSONSchema7,
} from './json-schema.d' // WARNING{DEPRECATED} - you should import from `@types/json-schema` directly
27 changes: 15 additions & 12 deletions json-ld.d.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
import { JSONValue, JSONObject, JSONArray } from './json.d'
import { JSONValue, JSONObject, JSONArray, JSONPrimitive } from './json.d'


/**
* A single JSON-LD document.
* @see https://github.com/json-ld/json-ld.org/blob/1.0/schemas/jsonld-schema.json
*/
export interface JSONLDObject extends CommonObject {
'@context'?: (ContextObject|string)[]|ContextObject|string|null;
'@graph'?: { [key: string]: CommonObject; }|CommonObject[];
[key: string]: any/*CommonObject*/;
export interface JSONLDDocument extends JSONLDObject {
'@context'?: Context[]|Context|null;
'@graph'?: { [key: string]: JSONLDObject; }|JSONLDObject[];
[key: string]: (JSONLDObject|JSONPrimitive)[]|JSONLDObject|JSONPrimitive|undefined;
}

type ContextObject = {
[key: string]: string|{
type Context = {
[key: string]: {
'@id': string;
'@type': '@id';
}
}
}|{
'@reverse': string;
}|string;
}|string

interface CommonObject extends JSONObject {
export interface JSONLDObject extends JSONObject {
/**
* @format uri
*/
Expand All @@ -29,13 +32,13 @@ interface CommonObject extends JSONObject {
'@id'?: string;
'@language'?: string|null;
'@list'?: JSONLDObject[];
'@reverse'?: { [key: string]: CommonObject; }|string|null;
'@reverse'?: { [key: string]: JSONLDObject; }|string|null;
'@set'?: JSONLDObject[];
'@type'?: string[]|string|null;
'@value'?: string|number|boolean|null;
/**
* @format uri
*/
'@vocab'?: string|null;
[key: string]: any/*CommonObject*/;
[key: string]: (JSONLDObject|JSONPrimitive)[]|JSONLDObject|JSONPrimitive|undefined;
}
148 changes: 8 additions & 140 deletions json-schema.d.ts
Original file line number Diff line number Diff line change
@@ -1,143 +1,11 @@
import { JSONValue, JSONObject, JSONArray } from './json.d'
// WARNING - this file is deprecated. you should import from `@types/json-schema` directly.

/**
* A single JSON Schema.
*/
export type JSONSchema = JSONSchemaObject|boolean

/**
* A JSON Schema, if that schema is an object (not a boolean).
*/
export interface JSONSchemaObject extends JSONObject {
$comment?: string;
/**
* @format 'uri-reference'
*/
$id?: string;
/**
* @format 'uri-reference'
*/
$ref?: string;
/**
* @format 'uri'
*/
$schema?: string;
additionalItems?: JSONSchema;
additionalProperties?: JSONSchema;
allOf?: schemaArray;
anyOf?: schemaArray;
const?: JSONValue;
contains?: JSONSchema;
contentEncoding?: string;
contentMediaType?: string;
default?: JSONValue;
/**
* @default {}
*/
definitions?: {
[key: string]: JSONSchema;
};
dependencies?: {
[key: string]: JSONSchema|stringArray;
},
description?: string;
else?: JSONSchema;
/**
* @minItems 1
* @uniqueItems true
*/
enum?: JSONArray;
examples?: JSONArray;
exclusiveMaximum?: number;
exclusiveMinimum?: number;
format?: string;
if?: JSONSchema;
/**
* @default true
*/
items?: JSONSchema|schemaArray;
maximum?: number;
maxItems?: nonNegativeInteger;
maxLength?: nonNegativeInteger;
maxProperties?: nonNegativeInteger;
minimum?: number;
minItems?: nonNegativeIntegerDefault0;
minLength?: nonNegativeIntegerDefault0;
minProperties?: nonNegativeIntegerDefault0;
/**
* @exclusiveMinimum 0
*/
multipleOf?: number;
not?: JSONSchema;
oneOf?: schemaArray;
/**
* @format regex
*/
pattern?: string;
/**
* @default {}
*/
patternProperties?: {
[
/**
* @format regex
*/
key: string
]: JSONSchema;
},
/**
* @default {}
*/
properties?: {
[key: string]: JSONSchema;
};
propertyNames?: JSONSchema;
/**
* @default false
*/
readOnly?: boolean;
required?: stringArray;
then?: JSONSchema;
title?: string;
/**
* @minItems 1
* @uniqueItems true
*/
type?: simpleTypes[]|simpleTypes;
/**
* @default false
*/
uniqueItems?: boolean;
}

/**
* @minItems 1
*/
type schemaArray = JSONSchema[]


/**
* @type integer
*/
type integer = number
import { JSONSchema7Definition, JSONSchema7 } from 'json-schema'

/**
* @type integer
* @minimum 0
*/
type nonNegativeInteger = integer

/**
* @type integer
* @minimum 0
* @default 0
*/
type nonNegativeIntegerDefault0 = integer

type simpleTypes = 'array'|'boolean'|'integer'|'null'|'number'|'object'|'string'

/**
* @uniqueItems true
* @default []
*/
type stringArray = string[]
export {
JSONSchema7Definition,
JSONSchema7,
JSONSchema7Definition as JSONSchema, // WARNING{DEPRECATED}
JSONSchema7 as JSONSchemaObject, // WARNING{DEPRECATED}
}
12 changes: 10 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@chharvey/requirejson",
"version": "1.0.1",
"version": "1.1.0",
"description": "Require JSON files with a file extension other than `.json`.",
"main": "index.js",
"scripts": {
Expand All @@ -20,5 +20,8 @@
"bugs": {
"url": "https://github.com/chharvey/requirejson/issues"
},
"homepage": "https://github.com/chharvey/requirejson#readme"
"homepage": "https://github.com/chharvey/requirejson#readme",
"dependencies": {
"@types/json-schema": "^7.0.2"
}
}

0 comments on commit 17c22b3

Please sign in to comment.