Just for a guy who wants to survive in a massive, legacy JavaScript/TypeScript codebase.
- πͺ JavaScript version of TypeScript + Zod: Ditch
any& complexity. - π£ No vender lock-in.
- π Find bug quickly.
- β€οΈβπ₯ DX first, DX first, DX first and security!
- πͺΆ ~4 kB (minified + gzipped), 0 dependencies.
- β Autocompletion support.
- β‘ (Optional) The definitive choice for high-performance.
npm add escss-estest- Core Concept
- Core API
- ESTest
- unSafeESTest
- Usage is exactly the same as ESTest
- ESTestForLibrary
- Usage is exactly the same as ESTest
- Helper API
- Thanks
ESTest: console.error --> decoupling / isESTestDisabled = true for high-performanceunSafeESTest: throw new ErrorESTestForLibrary: The default message is separated fromESTest&unSafeESTest
import { ESTest } from "escss-estest";
function sum(a, b) {
{
// validate type
ESTest(a, "number");
ESTest(b, "number");
}
// do something
}import { ESTest } from "escss-estest";
async function getApi() {
const apiData = await fetch("https://jsonplaceholder.typicode.com/users/1");
const data = await apiData.json();
// const data = {
// id: 1,
// name: "Leanne Graham",
// username: "Bret",
// email: "Sincere@april.biz",
// address: {
// street: "Kulas Light",
// suite: "Apt. 556",
// city: "Gwenborough",
// zipcode: "92998-3874",
// geo: {
// lat: "-37.3159",
// lng: "81.1496"
// }
// },
// phone: "1-770-736-8031 x56442",
// website: "hildegard.org",
// company: {
// name: "Romaguera-Crona",
// catchPhrase: "Multi-layered client-server neural-net",
// bs: "harness real-time e-markets"
// }
// }
{
// API validate from the backend failed -> console.error('your text')
// pass
ESTest(data, "object", "schema do not match").schema({
id: "number",
name: "string",
username: "string",
email: "string",
address: {
street: "string",
suite: "string",
city: "string",
zipcode: "string",
geo: {
lat: "string",
lng: "string",
},
},
phone: "string",
website: "string",
company: {
name: "string",
catchPhrase: "string",
bs: "string",
},
});
}
// do something
}
getApi();Usage is exactly the same as ESTest
import { unSafeESTest } from "escss-estest";
import express from "express";
const app = express();
const port = 3000;
app.use(express.json());
app.post("/api/login", (req, res) => {
try {
const data = req.body;
// const data = {
// username: 'abcd',
// password: '1234',
// confirmPassword: '1234'
// }
{
// Schema or password validation failed -> throw new Error('your text')
// pass
unSafeESTest(data, "object", "schema do not match")
.schema({
username: "string",
password: "string",
confirmPassword: "string",
})
.refine(
(val) => val.password === val.confirmPassword,
"passwords do not match",
);
}
// do something
res.json({ message: "ok" });
} catch (err) {
res.status(400).json({ message: err.message });
}
});
app.listen(port, () => {
console.log(`http://localhost:${port}`);
});Library's own default message
import { ESTestForLibrary } from "escss-estest";
function ESTest(
input,
type,
message = "[LibraryName] default message for others to help debugging",
) {
return ESTestForLibrary(input, type, message);
}- Set default message for your project.
globalThis.__ESCSS_ESTEST__.message = "Please report this issue to ...";true: Disable to get high-performance. (for production)false: Show Bug detail.
Note: unSafeESTest will not be affected (for security reasons)
globalThis.__ESCSS_ESTEST__.isESTestDisabled = true;
function sum(a, b) {
{
ESTest(a, "number");
ESTest(b, "number");
}
return a + b;
}
// same as
function sum(a, b) {
return a + b;
}- Show usage reports
