
Security News
Axios Supply Chain Attack Reaches OpenAI macOS Signing Pipeline, Forces Certificate Rotation
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.
nm-validator
Advanced tools
This package is DEPRECATED. The REPLACEMENT for is: https://www.npmjs.com/package/ts-validity
npm i nm-validator
or
yarn add nm-validator
Example 1 : Validate Object
import { validator } from "nm-validator";
import {
emailAddress,
equal,
minLength,
required,
regularExpression,
} from "nm-validator/dist/validationRules";
const company = {
name: "",
email: "irpan2gmail.com",
address: {
streetName: "",
country: "UK",
person: {
age: 15,
},
},
};
//Deep validation
const rules: ValidationRules<typeof company> = {
name: [required("Name is required")],
address: {
streetName: [required("The street name is required")],
country: [elementOf(["US,FR,JP,ID"])],
person: {
age: [minNumber(17)],
},
},
};
const validationResult = validator.validateObject(registrant, validationRule);
//Output validationResult:
// validationResult = {
// isValid: false,
// errorMessages: {
// name: ["Name is required."],
// address: {
// streetName: ["The street name is required."],
// country: ["The value 'UK' is not the element of [US,FR,JP,ID]."],
// person: {
// age: ["The minimum value for this field is 17."],
// },
// },
// },
// errors: {
// name: "Name is required.",
// address: {
// streetName: "The street name is required.",
// country: "The value 'UK' is not the element of [US,FR,JP,ID].",
// person: {
// age: "The minimum value for this field is 17.",
// },
// },
// },
// };
Example 2 : Validate a Spesific Field
import { validator } from "nm-validator";
import {
emailAddress,
equal,
minLength,
required,
regularExpression,
} from "nm-validator/dist/validationRules";
const registrant: Registrant = {
name: "",
email: "",
password: "",
confirmPassword: "",
};
const validationRule: ValidationRules<Registrant> = {
name: [required("Name is required"), minLength(3, "The minimum length is 3")],
email: [required("Email is required"), emailAddress("Invalid email address")],
};
//only validate the email field
const validationResult = validator.validateField(
registrant,
"email",
validationRule
);
//Output
// validationResult = {
// isValid: false,
// errorMessages: {
// email: [
// "Email is required",
// "Invalid email address"
// ]
// },
// errors: {
// email: "Email is required. Invalid email address."
// }
// }
Example 3 : Create and use your own field rule
import { validator } from "nm-validator";
//This is our custom field rule, should return the FieldValidator interface
const mustBePi: FieldRule = (errorMessage?: string) => {
const c = 3.14;
let msg = `The value must be ${c}`;
if (errorMessage) {
msg = errorMessage;
}
const validator: FieldValidator = {
errorMessage: msg,
validate: (value: any, objRef?: any): boolean => {
return value === c;
},
};
return validator;
};
const pi = {
value: 3.13,
};
const validationRule: ValidationRules = {
value: [mustBePi()],
};
const validationResult = validator.validateObject(pi, validationRule);
//Output
// validationResult = {
// isValid: false,
// errorMessages: {
// value: ["The value must be 3.14"],
// },
// errors: {
// value: "The value must be 3.14",
// },
// };
Example 4 : Deep validate spesific field
import { validator } from "nm-validator";
import {
emailAddress,
equal,
minLength,
required,
regularExpression,
} from "nm-validator/dist/validationRules";
const company = {
name: "",
email: "irpan2gmail.com",
address: {
streetName: "",
country: "UK",
person: {
age: 15,
},
},
};
const rules: ValidationRules<typeof company> = {
name: [required("Name is required")],
address: {
streetName: [required("The street name is required")],
country: [elementOf(["US,FR,JP,ID"])],
person: {
age: [minNumber(17)],
},
},
};
//Validate only the age value
const actual = validator.validateField(company, "address.person.age", rules);
//Output
// validationResult = {
// isValid: false,
// errorMessages: {
// address: {
// person: {
// age: ["The minimum value for this field is 17."],
// },
// },
// },
// errors: {
// address: {
// person: {
// age: "The minimum value for this field is 17.",
// },
// },
// },
// };
FAQs
Simple json validator by using user-defined validation rules
We found that nm-validator demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.

Security News
Open source is under attack because of how much value it creates. It has been the foundation of every major software innovation for the last three decades. This is not the time to walk away from it.

Security News
Socket CEO Feross Aboukhadijeh breaks down how North Korea hijacked Axios and what it means for the future of software supply chain security.