Skip to content

Commit

Permalink
v0.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
shuritch committed Oct 16, 2023
1 parent 70f4e35 commit 9a01cce
Show file tree
Hide file tree
Showing 23 changed files with 138 additions and 209 deletions.
35 changes: 10 additions & 25 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,16 @@

## [Unreleased][unreleased]

## [1.2.0][] - 2023-09-12

- Renamed from workspace -> node-workspace

## [1.1.1][] - 2023-08-23

- Astrohelm config update
- Packages update

## [1.1.0][] - 2023-08-02

- Prettier row length 120 -> 100
- Makefile git commands
- Grep command
- Replace command
- Search between files command
- Search between rows command

## [1.0.0][] - 2023-07-31
## [0.1.0][] - 2023-10-16

- Stable release version
- Warnings before testing
- Repository created

[unreleased]: https://github.com/astrohelm/workspace/compare/v1.2.0...HEAD
[1.1.1]: https://github.com/astrohelm/workspace/compare/v1.1.1...v1.2.0
[1.1.1]: https://github.com/astrohelm/workspace/compare/v1.1.0...v1.1.1
[1.1.0]: https://github.com/astrohelm/workspace/compare/release...v1.1.0
[1.0.0]: https://github.com/astrohelm/workspace/releases/tag/release
- Namespaces, runtime schema checking, custom types
- Default struct types: Array, Set, Map, Object
- Default scalar types: String, Boolean, Number, BigInt
- Default exotic types: Any, Undefined, JSON
- Custom Errors

[unreleased]: https://github.com/astrohelm/workspace/compare/release...HEAD
[0.1.0]: https://github.com/astrohelm/workspace/releases/tag/release
5 changes: 0 additions & 5 deletions dist/.eslintrc

This file was deleted.

16 changes: 0 additions & 16 deletions lib/custom/prototypes.js

This file was deleted.

7 changes: 0 additions & 7 deletions lib/custom/rules.js

This file was deleted.

5 changes: 4 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
'use strict';

module.exports = function () {};
const prototypes = require('./proto');
const Schema = require('./schema');

module.exports = { Schema, prototypes };
File renamed without changes.
2 changes: 1 addition & 1 deletion lib/types/proto/arrays.js → lib/proto/arrays.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const array = {
kind: 'struct',
isInstance: value => Array.isArray(value),
construct(schema, tools) {
const { Error, builders, conditions } = tools.builder;
const { Error, builders, conditions } = tools;
const builded = builders.array(schema, tools);
const { type, condition = 'anyof', required } = schema;
return (sample, path) => {
Expand Down
9 changes: 0 additions & 9 deletions lib/proto/enum.js

This file was deleted.

4 changes: 2 additions & 2 deletions lib/types/proto/exotic.js → lib/proto/exotic.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const any = type => ({
kind: 'scalar',
type,
construct(schema, tools) {
const { isRequired, Error } = tools.builder;
const { isRequired, Error } = tools;
const required = isRequired(schema);
return (sample, path) => {
if (!(required && sample === undefined)) return [];
Expand All @@ -16,7 +16,7 @@ const any = type => ({
const json = {
kind: 'struct',
construct: (schema, tools) => {
const { Error } = tools.builder;
const { Error } = tools;
return (sample, path) => {
if (typeof sample === 'object' && sample) return [];
return [new Error({ path, sample, schema, cause: 'Not of expected type: object' })];
Expand Down
16 changes: 5 additions & 11 deletions lib/proto/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
'use strict';
const PROTO = {
...require('../types/proto/scalars'),
enum: require('./enum'),
};

const getPrototype = plan => {
const isShorthand = typeof plan === 'string';
let type = isShorthand ? plan : plan.type;
if (isShorthand) type = type.substring(1);
return PROTO[type](plan);
module.exports = {
...require('./scalars'),
...require('./objects'),
...require('./arrays'),
...require('./exotic'),
};

module.exports = { PROTO, getPrototype };
2 changes: 1 addition & 1 deletion lib/types/proto/objects.js → lib/proto/objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const object = {
entries: value => Object.entries(value),
isInstance: value => typeof value === 'object',
construct(schema, tools) {
const { Error, builders } = tools.builder;
const { Error, builders } = tools;
const { search, required } = builders['object'](schema, tools);
return (sample, path) => {
const err = cause => new Error({ path, sample, schema, cause });
Expand Down
2 changes: 1 addition & 1 deletion lib/types/proto/scalars.js → lib/proto/scalars.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const scalar = type => {
kind: 'scalar',
parse: Object.assign(parse, { isCompatible, targets: [type] }),
construct(schema, tools) {
const { isRequired, Error } = tools.builder;
const { isRequired, Error } = tools;
const required = isRequired(schema);
return (sample, path) => {
if (typeof sample === type) return [];
Expand Down
22 changes: 22 additions & 0 deletions lib/schema/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict';

const preprocess = require('./preprocess');
module.exports = (types, tools, schema) => {
const tests = preprocess(types, tools, schema);
const { typeCondition = 'anyof' } = schema;
return (sample, path = 'root') => {
const handler = tools.conditions(typeCondition, tests.length - 1);
const errors = [];
for (let i = 0; i < tests.length; ++i) {
const result = tests[i](sample, path);
const [toDo, err] = handler(result, i);
if (err) {
if (result.length) errors.push(...result);
else errors.push(`[${path}] => ${err}: ${JSON.stringify(sample)}`);
}
if (toDo === 'skip' || toDo === 'break') break;
if (toDo === 'continue') continue;
}
return errors;
};
};
File renamed without changes.
36 changes: 17 additions & 19 deletions lib/schema/index.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,24 @@
'use strict';

const schemaFrom = require('./from');
const prototypes = require('../proto');
const arrayBuilder = (schema, { build }) => schema.items.map(v => build(v));
const [objectBuilder, createError] = [require('./object'), require('./error')];
const builders = { object: objectBuilder, array: arrayBuilder };
const tooling = { Error: createError(), ...require('./utils'), builders };
const build = require('./build');

//TODO Type fabric, to fix existed and passed types
//TODO Parser that creates test for Schema, append Models, and tests it for errors
const Schema = function (plan, options = {}) {
const { namespace, types, rules } = options;
module.exports = function Schema(schema, { errorPattern, ...options }) {
const tools = { ...tooling, build: null, warn: null };
if (errorPattern) tools.Error = createError({ pattern: errorPattern });
const warn = options => {
const err = new tooling.Error(options);
return this.warnings.push(err), err;
};

this.plan = plan;
this.test = sample => {
const isShorthand = typeof plan === 'string';
let planType = isShorthand ? plan : plan.type;
if (isShorthand) {
if (planType[0] === '?') planType = planType.substring(1);
const errors = prototypes[planType](sample);
}
[tools.build, tools.warn] = [build.bind(null, options, tools), warn];

const result = { valid: true, errors: [] };
return result;
};
this.warnings = [];
this.test = tools.build(schema);
return Object.freeze(this);
};

module.exports = Schema;
module.exports.from = sample => new Schema(schemaFrom(sample));
// schema check, dts generation,
6 changes: 3 additions & 3 deletions lib/types/builder/object.js → lib/schema/object.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
'use strict';

module.exports = (schema, { builder }) => {
module.exports = (schema, { build, isRequired }) => {
const builded = { properties: {}, patternProperties: {} };
const requires = new Map();

for (const propType in builded) {
if (!schema[propType]) continue;
const entries = Object.entries(schema[propType]);
for (const [key, value] of entries) {
builded[propType][key] = builder.build(value);
const required = builder.isRequired(value);
builded[propType][key] = build(value);
const required = isRequired(value);
if (required) requires.set(key);
}
}
Expand Down
20 changes: 12 additions & 8 deletions lib/types/preprocess.js → lib/schema/preprocess.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
'use strict';

const path = 'PREPROCESS';
const { string } = require('astropack');
const { typeOf, isShorthand } = require('./utils');
module.exports = (schema, types, tools) => {
const tests = [];
const path = 'PREPROCESS';

const { warn } = tools;
module.exports = ({ types, namespace }, tools, schema) => {
const [tests, { warn }] = [[], tools];
const signal = (cause, sample, sampleType) => warn({ sample, sampleType, path, cause });

for (const type of typeOf(schema)) {
if (string.case.isFirstUpper(type)) {
const prototype = namespace[type];
if (prototype) {
tests.push(prototype.test);
continue;
}
}
const prototype = types[type];
if (!prototype) {
const err = signal('Missing prototype', prototype, type);
Expand All @@ -20,9 +26,7 @@ module.exports = (schema, types, tools) => {
tests.push(() => [err]);
continue;
}

const test = prototype.construct(schema, tools);
tests.push(test);
tests.push(prototype.construct(schema, tools));
}
return tests;
};
File renamed without changes.
41 changes: 0 additions & 41 deletions lib/types/builder/index.js

This file was deleted.

6 changes: 0 additions & 6 deletions lib/types/index.js

This file was deleted.

8 changes: 0 additions & 8 deletions lib/types/proto/index.js

This file was deleted.

29 changes: 16 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
{
"license": "MIT",
"version": "1.2.0",
"version": "0.1.0",
"type": "commonjs",
"name": "astrohelm-workspace",
"name": "astroplan",
"homepage": "https://astrohelm.ru",
"description": "Astrohelm workspace example",
"author": "Alexander Ivanov <[email protected]>",
"keywords": [
"nodejs",
"zero-dependencies",
"prettier",
"eslint",
"ts",
"workspace",
"example",
"preset",
"starter-kit",
"astrohelm",
"javascript",
"typescript"
"testing",
"metadata",
"json",
"schema",
"validation",
"types",
"checker",
"dsl",
"metalanguage",
"runtime-verification",
"zero-dependencies",
"type-generator",
"astrohelm"
],
"main": "index.js",
"types": "types/index.d.ts",
Expand All @@ -28,7 +31,7 @@
"node": "18 || 19 || 20"
},
"browser": {},
"files": ["/dist", "/lib", "/types"],
"files": ["/lib", "/types"],
"scripts": {
"test": "node --test",
"dev": "node index.js",
Expand Down
Loading

0 comments on commit 9a01cce

Please sign in to comment.