Skip to content

Commit

Permalink
chore: updated effect
Browse files Browse the repository at this point in the history
  • Loading branch information
jessekelly881 committed Oct 3, 2023
1 parent 732c618 commit acd9e85
Show file tree
Hide file tree
Showing 15 changed files with 66 additions and 177 deletions.
5 changes: 5 additions & 0 deletions .changeset/lucky-readers-call.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"effect-schema-compilers": patch
---

updated effect packages
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,11 @@
"vitest": "^0.29.3"
},
"dependencies": {
"@effect/data": "^0.17.5",
"@effect/io": "^0.38.2",
"@effect/printer": "^0.13.0",
"@effect/schema": "^0.33.1",
"@effect/schema": "^0.39.2",
"@effect/typeclass": "^0.3.0",
"fast-check": "^3.11.0",
"effect": "2.0.0-next.42",
"fast-check": "^3.13.1",
"typescript": "^5.0.4"
}
}
66 changes: 24 additions & 42 deletions pnpm-lock.yaml

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

22 changes: 4 additions & 18 deletions src/common.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import * as AST from "@effect/schema/AST"
import * as S from "@effect/schema/Schema"
import { isNumber } from "@effect/data/Predicate"
import { isNumber } from "effect/Predicate"

export const createHookId = <I extends string>(id: I) => `effect-schema-compilers/${id}` as const
export const createHookId = (id: string) => Symbol(`effect-schema-compilers/${id}`)

/**
* TODO: Replace with import from "@effect/schema/internal/common" when working
Expand Down Expand Up @@ -87,14 +87,6 @@ export const getConstraints = (ast: AST.Refinement): Constraints | undefined =>
});
case S.LessThanOrEqualToTypeId:
return new NumberConstraints({ max: jsonSchema.maximum });
case S.PositiveTypeId:
return new NumberConstraints({ exclusiveMin: 0 });
case S.NonNegativeTypeId:
return new NumberConstraints({ min: 0 });
case S.NegativeTypeId:
return new NumberConstraints({ exclusiveMax: 0 });
case S.NonPositiveTypeId:
return new NumberConstraints({ max: 0 });
case S.IntTypeId:
return new NumberConstraints({ isInt: true });
case S.BetweenTypeId:
Expand All @@ -116,21 +108,15 @@ export const getConstraints = (ast: AST.Refinement): Constraints | undefined =>
});
case S.LessThanOrEqualToBigintTypeId:
return new BigintConstraints({ max: jsonSchema.maximum });
case S.PositiveBigintTypeId:
return new BigintConstraints({ exclusiveMin: 0n });
case S.NonNegativeBigintTypeId:
return new BigintConstraints({ min: 0n });
case S.NegativeBigintTypeId:
return new BigintConstraints({ exclusiveMax: 0n });
case S.NonPositiveBigintTypeId:
return new BigintConstraints({ max: 0n });
case S.BetweenBigintTypeId:
return new BigintConstraints({
min: jsonSchema.minimum,
max: jsonSchema.maximum,
});

// String
case S.LengthTypeId:
return new StringConstraints({ minLength: jsonSchema.minLength, maxLength: jsonSchema.maxLength });
case S.MinLengthTypeId:
return new StringConstraints({ minLength: jsonSchema.minLength });
case S.MaxLengthTypeId:
Expand Down
4 changes: 2 additions & 2 deletions src/empty.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as S from "@effect/schema/Schema"
import * as AST from "@effect/schema/AST"
import * as RA from "@effect/data/ReadonlyArray"
import * as O from "@effect/data/Option"
import * as RA from "effect/ReadonlyArray"
import * as O from "effect/Option"
import { Constraints, combineConstraints, getConstraints, createHookId } from "./common"


Expand Down
8 changes: 3 additions & 5 deletions src/equivalence.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import * as S from "@effect/schema/Schema"
import * as AST from "@effect/schema/AST"
import * as Eq from "@effect/data/Equivalence";
import { pipe } from "@effect/data/Function"
import * as O from "@effect/data/Option"
import * as RA from "@effect/data/ReadonlyArray"
import * as Eq from "effect/Equivalence";
import * as O from "effect/Option"
import * as RA from "effect/ReadonlyArray"
import { createHookId, memoizeThunk } from "./common"
import * as Equal from "@effect/data/Equal";


export const EquivalenceHookId = createHookId("EquivalenceHookId")
Expand Down
9 changes: 4 additions & 5 deletions src/faker.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import * as S from "@effect/schema/Schema"
import * as AST from "@effect/schema/AST"
import type * as F from '@faker-js/faker';
import * as O from "@effect/data/Option"
import { pipe } from "@effect/data/Function";
import * as RA from "@effect/data/ReadonlyArray"
import * as O from "effect/Option"
import * as RA from "effect/ReadonlyArray"
import { Constraints, combineConstraints, createHookId, getConstraints, memoizeThunk } from "./common"
import { isBigint, isNumber } from "@effect/data/Predicate";
import { isBigint, isNumber } from "effect/Predicate";

export const FakerHookId = createHookId("FakerHookId")

Expand Down Expand Up @@ -92,7 +91,7 @@ const go = (ast: AST.AST, depthLimit = 10, constraints?: Constraints): Faker<any
const max = c.constraints.maxLength
const pattern = c.constraints.pattern;

return c.constraints.pattern ? f.helpers.fromRegExp(pattern) : f.string.sample({ min, max })
return pattern ? f.helpers.fromRegExp(pattern) : f.string.sample({ min, max })
}

return f.string.sample()
Expand Down
6 changes: 3 additions & 3 deletions src/jsonSchema.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import * as O from "@effect/data/Option"
import * as RA from "@effect/data/ReadonlyArray"
import * as O from "effect/Option"
import * as RA from "effect/ReadonlyArray"
import * as AST from "@effect/schema/AST"
import type { Schema } from "@effect/schema/Schema"
import * as S from "@effect/schema/Schema"
import { pipe } from "@effect/data/Function"
import { pipe } from "effect/Function"

export type JsonSchema7AnyType = {}

Expand Down
4 changes: 2 additions & 2 deletions src/semigroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import * as S from "@effect/schema/Schema"
import * as Semi from "@effect/typeclass/Semigroup"
import * as AST from "@effect/schema/AST"
import { createHookId, memoizeThunk } from "./common"
import * as RA from "@effect/data/ReadonlyArray"
import * as O from "@effect/data/Option"
import * as RA from "effect/ReadonlyArray"
import * as O from "effect/Option"

export const SemigroupHookId = createHookId("SemigroupHookId")

Expand Down
11 changes: 4 additions & 7 deletions tests/empty.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { describe, it, expect } from "vitest";
import * as S from "@effect/schema/Schema"
import { pipe } from "@effect/data/Function"
import { pipe } from "effect/Function"
import * as _ from "../src/empty"
import * as Eq from "../src/equivalence"
import * as fc from 'fast-check'
import * as A from "@effect/schema/Arbitrary";
import { Category, Fruits } from "./common";

const expectEmptyValues = <A, I>(schema: S.Schema<I, A>, from: I, to: A) => {
Expand All @@ -21,7 +18,7 @@ describe("empty", () => {
it("any", () => expectEmptyValues(S.any, undefined, undefined))
it("unknown", () => expectEmptyValues(S.unknown, undefined, undefined))
it("number", () => expectEmptyValues(S.number, 0, 0))
it("bigint", () => expectEmptyValues(S.bigint, 0n, 0n))
it("bigint", () => expectEmptyValues(S.bigint, "", 0n))
it("string", () => expectEmptyValues(S.string, "", ""));
it("boolean", () => expectEmptyValues(S.boolean, false, false));
it("enum", () => expectEmptyValues(S.enums(Fruits), Fruits.Apple, Fruits.Apple))
Expand All @@ -40,8 +37,8 @@ describe("empty", () => {
it("number/ greaterThan", () => expectEmptyValues(pipe(S.number, S.greaterThan(4)), 0, 4))
it("number/ greaterThanOrEqualTo", () => expectEmptyValues(pipe(S.number, S.greaterThanOrEqualTo(4)), 0, 4))
it("number/ int, greaterThan", () => expectEmptyValues(pipe(S.number, S.int(), S.greaterThan(4)), 0, 5))
it("bigint/ greaterThan", () => expectEmptyValues(pipe(S.bigint, S.greaterThanBigint(4n)), 0n, 5n))
it("bigint/ greaterThanOrEqualTo", () => expectEmptyValues(pipe(S.bigint, S.greaterThanOrEqualToBigint(4n)), 0n, 4n))
it("bigint/ greaterThan", () => expectEmptyValues(pipe(S.bigint, S.greaterThanBigint(4n)), "", 5n))
it("bigint/ greaterThanOrEqualTo", () => expectEmptyValues(pipe(S.bigint, S.greaterThanOrEqualToBigint(4n)), "", 4n))
it("string/ minLength", () => expectEmptyValues(pipe(S.string, S.minLength(2)), "", " "))
it("array/ minItems", () => expectEmptyValues(pipe(S.array(S.string), S.minItems(2)), [], ["", ""]))

Expand Down
4 changes: 2 additions & 2 deletions tests/equivalence.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { describe, it, expect } from "vitest";
import * as S from "@effect/schema/Schema"
import { pipe } from "@effect/data/Function"
import { pipe } from "effect/Function"
import * as _ from "../src/equivalence"
import * as Eq from "@effect/data/Equivalence";
import * as Eq from "effect/Equivalence";
import * as fc from 'fast-check'
import * as A from "@effect/schema/Arbitrary";
import { Category, Fruits } from "./common";
Expand Down
11 changes: 7 additions & 4 deletions tests/faker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { describe, expect, it } from "vitest";
import * as S from "@effect/schema/Schema"
import * as _ from "../src/faker";
import * as F from '@faker-js/faker';
import { pipe } from "@effect/data/Function";
import { pipe } from "effect/Function";
import { Category, Fruits } from "./common";


Expand All @@ -11,7 +11,10 @@ import { Category, Fruits } from "./common";
*/
const generatesValidValue = <I, A>(schema: S.Schema<I, A>) => {
const fake = _.to(schema)(F.faker)
expect(S.is(schema)(fake)).to.be.true
const isValid = S.is(schema)(fake)
if(!isValid) console.log(fake)

expect(isValid).to.be.true
}

describe("faker", () => {
Expand Down Expand Up @@ -48,7 +51,7 @@ describe("faker", () => {
it("number/ int (0, 5)", () => generatesValidValue(pipe(S.number, S.int(), S.greaterThan(0), S.lessThan(5))))
it("number/ int [0, 5]", () => generatesValidValue(pipe(S.number, S.int(), S.greaterThanOrEqualTo(0), S.lessThanOrEqualTo(5))))
it("bigint/ (0, 5)", () => generatesValidValue(pipe(S.bigint, S.greaterThanBigint(0n), S.lessThanBigint(5n))))
it("string/ length", () => generatesValidValue(pipe(S.string, S.length(30))))
it("string/ length", () => generatesValidValue(pipe(S.string, S.length(10))))
it("string/ minLength, maxLength", () => generatesValidValue(pipe(S.string, S.minLength(30), S.maxLength(50))))
it("string/ pattern", () => generatesValidValue(pipe(S.string, S.pattern(/hello-[1-5]/))))
it("array/ itemsCount", () => generatesValidValue(pipe(S.array(S.string), S.itemsCount(10))))
Expand Down Expand Up @@ -97,7 +100,7 @@ describe("faker", () => {
})

it("struct - extra props", () => {
const schema = pipe(S.struct({ a: S.string, b: S.number }), S.extend(S.record(S.symbol, S.string)))
const schema = pipe(S.struct({ a: S.symbol, b: S.number }), S.extend(S.record(S.string, S.string)))
generatesValidValue(schema)
})

Expand Down
2 changes: 1 addition & 1 deletion tests/jsonSchema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as A from "@effect/schema/Arbitrary";
import * as S from "@effect/schema/Schema";
import * as fc from "fast-check";
import * as _ from "../src/jsonSchema";
import { isRecord } from "@effect/data/Predicate";
import { isRecord } from "effect/Predicate";
import Ajv from "ajv";

const isJsonArray = (u: unknown): u is _.JsonArray => Array.isArray(u) && u.every(isJson)
Expand Down
Loading

0 comments on commit acd9e85

Please sign in to comment.