Skip to content

Commit

Permalink
rc2 with parseJsonOrThrow
Browse files Browse the repository at this point in the history
  • Loading branch information
DZakh committed Dec 15, 2024
1 parent 255a0ac commit e2206e4
Show file tree
Hide file tree
Showing 14 changed files with 40 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ jobs:
uses: actions/upload-artifact@v3
with:
name: rescript-schema-ppx
path: packages/rescript-schema-ppx/rescript-schema-ppx-9.0.0.tgz
path: packages/rescript-schema-ppx/rescript-schema-ppx-9.0.1.tgz

benchmark:
name: Benchmark
Expand Down
2 changes: 0 additions & 2 deletions IDEAS.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ let trimContract: S.contract<string => string> = S.contract(s => {

## v9

- parseJsonOrThrow
- convertReverse to serialise?
- Update documentation:
- Add Enums section to js docs

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rescript-schema",
"version": "9.0.0-rc.1",
"version": "9.0.0-rc.2",
"description": "🧬 The fastest parser in the entire JavaScript ecosystem with a focus on small bundle size and top-notch DX",
"keywords": [
"ReScript",
Expand Down
4 changes: 2 additions & 2 deletions packages/rescript-schema-ppx/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rescript-schema-ppx",
"version": "9.0.0",
"version": "9.0.1",
"description": "ReScript PPX to generate rescript-schema from type",
"keywords": [
"ReScript",
Expand Down Expand Up @@ -32,6 +32,6 @@
"postinstall": "node ./install.cjs"
},
"peerDependencies": {
"rescript-schema": "9 || 9.0.0-rc.1"
"rescript-schema": "^9.0.0 || 9.0.0-rc.1 || 9.0.0-rc.2"
}
}
14 changes: 14 additions & 0 deletions packages/tests/src/core/S_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1834,6 +1834,20 @@ test("Example", (t) => {
expectType<TypeEqual<LoginData, { email: string; password: string }>>(true);
});

test("parseJsonOrThrow", async (t) => {
const schema = S.nullable(S.string);

t.deepEqual(S.parseJsonOrThrow("hello", schema), "hello");
t.deepEqual(S.parseJsonOrThrow(null, schema), undefined);
});

test("parseJsonStringOrThrow", async (t) => {
const schema = S.nullable(S.string);

t.deepEqual(S.parseJsonStringOrThrow(`"hello"`, schema), "hello");
t.deepEqual(S.parseJsonStringOrThrow("null", schema), undefined);
});

test("Compile types", async (t) => {
const schema = S.nullable(S.string);

Expand Down
2 changes: 1 addition & 1 deletion packages/tests/src/core/S_union_test.res
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,7 @@ module CknittelBugReport2 = {
t->U.assertCompiledCode(
~schema,
~op=#Parse,
`i=>{let v3=i;if(!i||i.constructor!==Object){e[10](i)}else{try{let v0=i["statusCode"];if(!v0||v0.constructor!==Object||v0["kind"]!=="ok"||0){e[0](v0)}let v1=v0["text"];try{if(v1!==""){e[2](v1)}}catch(v2){if(v2&&v2.s===s){v1=e[1](v1,v2)}else{throw v2}}v3={"TAG":e[3],"_0":e[4],}}catch(e0){try{let v4=i["statusCode"];if(!v4||v4.constructor!==Object||v4["kind"]!=="serviceError"){e[5](v4)}let v5=v4["serviceCode"],v6=v4["text"];if(typeof v5!=="string"){e[6](v5)}if(typeof v6!=="string"){e[7](v6)}v3={"TAG":e[8],"_0":{"serviceCode":v5,"text":v6,},}}catch(e1){e[9]([e0,e1,])}}}return v3}`,
`i=>{let v3=i;if(!i||i.constructor!==Object){e[10](i)}else{try{let v0=i["statusCode"];if(!v0||v0.constructor!==Object||v0["kind"]!=="ok"||false){e[0](v0)}let v1=v0["text"];try{if(v1!==""){e[2](v1)}}catch(v2){if(v2&&v2.s===s){v1=e[1](v1,v2)}else{throw v2}}v3={"TAG":e[3],"_0":e[4],}}catch(e0){try{let v4=i["statusCode"];if(!v4||v4.constructor!==Object||v4["kind"]!=="serviceError"){e[5](v4)}let v5=v4["serviceCode"],v6=v4["text"];if(typeof v5!=="string"){e[6](v5)}if(typeof v6!=="string"){e[7](v6)}v3={"TAG":e[8],"_0":{"serviceCode":v5,"text":v6,},}}catch(e1){e[9]([e0,e1,])}}}return v3}`,
)

t->Assert.deepEqual(
Expand Down
2 changes: 1 addition & 1 deletion pnpm-lock.yaml

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

3 changes: 3 additions & 0 deletions src/S.bs.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ var compile = S_Core$RescriptSchema.compile;

var parseOrThrow = S_Core$RescriptSchema.parseOrThrow;

var parseJsonOrThrow = S_Core$RescriptSchema.parseJsonOrThrow;

var parseJsonStringOrThrow = S_Core$RescriptSchema.parseJsonStringOrThrow;

var parseAsyncOrThrow = S_Core$RescriptSchema.parseAsyncOrThrow;
Expand Down Expand Up @@ -222,6 +224,7 @@ export {
to ,
compile ,
parseOrThrow ,
parseJsonOrThrow ,
parseJsonStringOrThrow ,
parseAsyncOrThrow ,
convertOrThrow ,
Expand Down
8 changes: 8 additions & 0 deletions src/S.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,14 @@ export function parseOrThrow<Output, Input>(
data: unknown,
schema: Schema<Output, Input>
): Output;
export function parseJsonOrThrow<Output, Input>(
json: Json,
schema: Schema<Output, Input>
): Output;
export function parseJsonStringOrThrow<Output, Input>(
jsonString: string,
schema: Schema<Output, Input>
): Output;
export function parseAsyncOrThrow<Output, Input>(
data: unknown,
schema: Schema<Output, Input>
Expand Down
2 changes: 2 additions & 0 deletions src/S.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ export const reverseConvertToJsonOrThrow = S.reverseConvertToJsonOrThrow;
export const reverseConvertToJsonStringOrThrow =
S.reverseConvertToJsonStringOrThrow;
export const parseOrThrow = S.parseOrThrow;
export const parseJsonOrThrow = S.parseJsonOrThrow;
export const parseJsonStringOrThrow = S.parseJsonStringOrThrow;
export const parseAsyncOrThrow = S.parseAsyncOrThrow;
export const assertOrThrow = S.assertOrThrow;
export const recursive = S.recursive;
Expand Down
1 change: 1 addition & 0 deletions src/S.resi
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ let compile: (
) => 'input => 'output

let parseOrThrow: ('any, t<'value>) => 'value
let parseJsonOrThrow: (Js.Json.t, t<'value>) => 'value
let parseJsonStringOrThrow: (string, t<'value>) => 'value
let parseAsyncOrThrow: ('any, t<'value>) => promise<'value>

Expand Down
3 changes: 3 additions & 0 deletions src/S_Core.bs.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3581,6 +3581,8 @@ var jsonString = factory$4;

var union = factory$5;

var parseJsonOrThrow = parseOrThrow;

var Schema = {};

var schema$7 = factory$6;
Expand Down Expand Up @@ -3669,6 +3671,7 @@ export {
to ,
compile$1 as compile,
parseOrThrow ,
parseJsonOrThrow ,
parseJsonStringOrThrow ,
parseAsyncOrThrow ,
convertOrThrow ,
Expand Down
2 changes: 2 additions & 0 deletions src/S_Core.res
Original file line number Diff line number Diff line change
Expand Up @@ -1360,6 +1360,8 @@ let parseOrThrow = (any, schema) => {
(schema->operationFn(Flag.typeValidation))(any)
}

let parseJsonOrThrow = parseOrThrow->Obj.magic

let parseJsonStringOrThrow = (jsonString: string, schema: t<'value>): 'value => {
try {
jsonString->Js.Json.parseExn
Expand Down
1 change: 1 addition & 0 deletions src/S_Core.resi
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ let compile: (
) => 'input => 'output

let parseOrThrow: ('any, t<'value>) => 'value
let parseJsonOrThrow: (Js.Json.t, t<'value>) => 'value
let parseJsonStringOrThrow: (string, t<'value>) => 'value
let parseAsyncOrThrow: ('any, t<'value>) => promise<'value>

Expand Down

0 comments on commit e2206e4

Please sign in to comment.