Skip to content

Commit

Permalink
Expose deepStrict and deepStrip to js API
Browse files Browse the repository at this point in the history
  • Loading branch information
DZakh committed Dec 6, 2024
1 parent e514fd3 commit c9b78ca
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 1 deletion.
1 change: 0 additions & 1 deletion IDEAS.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ let trimContract: S.contract<string => string> = S.contract(s => {

## v9

- deepStrict for js/ts
- Update documentation:
- Add Enums section to js docs

Expand Down
38 changes: 38 additions & 0 deletions packages/tests/src/core/S_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -916,6 +916,44 @@ test("Fails to parse strict object with exccess fields", (t) => {
);
});

test("Fails to parse deep strict object with exccess fields", (t) => {
const schema = S.deepStrict(
S.schema({
foo: {
a: S.string,
},
})
);

t.throws(
() => {
const value = S.parseOrThrow(
{
foo: {
a: "bar",
b: true,
},
},
schema
);
expectType<
SchemaEqual<
typeof schema,
{
foo: {
a: string;
};
}
>
>(true);
},
{
name: "RescriptSchemaError",
message: `Failed parsing at ["foo"]. Reason: Encountered disallowed excess key "b" on an object`,
}
);
});

test("Fails to parse strict object with exccess fields which created using global config override", (t) => {
S.setGlobalConfig({
defaultUnknownKeys: "Strict",
Expand Down
6 changes: 6 additions & 0 deletions src/S.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,15 @@ export function object<Output, Input extends Record<string, unknown>>(
export function strip<Output, Input extends Record<string, unknown>>(
schema: Schema<Output, Input>
): Schema<Output, Input>;
export function deepStrip<Output, Input extends Record<string, unknown>>(
schema: Schema<Output, Input>
): Schema<Output, Input>;
export function strict<Output, Input extends Record<string, unknown>>(
schema: Schema<Output, Input>
): Schema<Output, Input>;
export function deepStrict<Output, Input extends Record<string, unknown>>(
schema: Schema<Output, Input>
): Schema<Output, Input>;

export function merge<O1, O2>(
schema1: Schema<O1, Record<string, unknown>>,
Expand Down
2 changes: 2 additions & 0 deletions src/S.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ export const assertOrThrow = S.assertOrThrow;
export const recursive = S.recursive;
export const merge = S.js_merge;
export const strict = S.strict;
export const deepStrict = S.deepStrict;
export const strip = S.strip;
export const deepStrip = S.deepStrip;
export const custom = S.js_custom;
export const tuple = S.tuple;
export const asyncParserRefine = S.js_asyncParserRefine;
Expand Down

0 comments on commit c9b78ca

Please sign in to comment.